0

我有一个带有列表视图的活动,列表视图是在列表视图适配器中创建的,所以在另一个类中。如何设置 onItemClick 上的多个操作仅针对每个列表项触发一次?我想更改单击的列表项的图像源,如果在 listView 适配器中设置,我如何从我的活动中访问它?

以下是 List View Adapter 中的一些代码,其中设置了 ListView 中的 Items:

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

 //.....Content is set with JSONObject from databse



 public class ListCell {
    private TextView note;
    private ImageView img;
}
4

1 回答 1

0

向您的 ListCell 类添加一个布尔字段,以便您可以识别项目何时被触摸。您将需要编写适当的 getter 和 setter。

 public class ListCell {
    private TextView note;
    private ImageView img;
    private boolean touched;
}

无论您在哪里使用您的onItemClick()onTouchEvent()您覆盖的任何方法来处理点击,您都可以在其中包含一些这样的逻辑:

if(!listCell.touched) {
  //do whatever needs to happen when the item is clicked
  listCell.setTouched(true);
}

在某些时候,如果您希望允许用户稍后再次单击它们,您还需要重置所有列表项的布尔值,因此请记住listCell.setTouched(false);在允许用户再次单击项目之前执行类似操作。

于 2015-06-05T19:14:35.380 回答