我有一个带有CheckBox和TextView项的ListView的Activity。当我第一次检查或最后取消选中CheckBox时,我想要一个带有一些按钮的LinearLayout来显示/隐藏。这没问题。问题是每当发生这种情况时,ListView项目顺序就会反转。这种行为只发生在我没有选中复选框并检查一个或当我最后一个选中取消选中它时。
我的适配器getView代码如下:
@Override
public View getView(int position, View view, ViewGroup parent)
{
LinearLayout v = (LinearLayout)view;
ViewHolder holder;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater)container.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.serverlist_view, null);
holder = new ViewHolder();
holder.checkbox = (CheckBox)v.findViewById(R.id.serverlist_checkbox);
holder.textview = (TextView) v.findViewById(R.id.serverlist_name);
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
LinearLayout layout = (LinearLayout)container.findViewById(R.id.buttons_linearlayout);
int visibility = View.VISIBLE;
if (isChecked)
{
nChecked++;
}
else
{
nChecked--;
if (nChecked == 0)
{
visibility = View.GONE;
}
}
layout.setVisibility(visibility);
}
});
v.setTag(holder);
}
else
{
holder = (ViewHolder)v.getTag();
}
holder.textview.setText(hostname);
return v;
}
如果我注释掉 layout.setVisibility(visibility),问题就会消失。我已经查看了这篇文章ListView list is reversed when selection made,但我无法使用我的代码。
有什么想法吗?
由于