将前一行存储在类变量中:
View previous;
...
public void onItemClick(AdapterView<?> list, View view, int position, long id) {
if(previous != null)
previous.setVisibility(View.INVISIBLE);
// Set the current button to visible while saving it for the next click
previous = view.findViewById(R.id.button);
previous.setVisibility(View.VISIBLE);
}
从评论添加
你是对的。适配器的视图回收正在影响其他行,所以让我们扩展您正在使用的任何适配器并覆盖它的getView()
方法:
public int selectedRow = -1;
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
// Here, either use findViewById() (slower) or the ViewHolder method (faster) to load the button:
// Button button = ...
if(position == selectedRow)
button.setVisibility(View.VISIBLE);
else
button.setVisibility(View.GONE);
return view;
}
在你的onItemClick()
方法中添加这样的一行:
adapter.selectedRow = position;