OnItem只为每个项目点击一次

时间:2015-06-05 18:39:47

标签: android listview onitemclicklistener listview-adapter

我有一个列表视图的活动,列表视图在列表视图适配器中创建,因此在另一个类中。 如何设置onItemClick上的几个操作仅针对每个列表项触发一次? 我想更改已单击的列表项的图像源,如果在listView 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;
}

1 个答案:

答案 0 :(得分: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);之类的操作。物品。

相关问题