Android 每个项目只需单击一次
我有一个带有列表视图的活动,列表视图是在列表视图适配器中创建的,所以在另一个类中。 如何设置每个列表项只触发一次onItemClick上的多个操作? 我想更改单击的列表项的图像源,如果在listView适配器中设置了,如何从活动中访问它 以下是列表视图适配器中的一些代码,其中设置了列表视图中的项目:Android 每个项目只需单击一次,android,listview,onitemclicklistener,listview-adapter,Android,Listview,Onitemclicklistener,Listview Adapter,我有一个带有列表视图的活动,列表视图是在列表视图适配器中创建的,所以在另一个类中。 如何设置每个列表项只触发一次onItemClick上的多个操作? 我想更改单击的列表项的图像源,如果在listView适配器中设置了,如何从活动中访问它 以下是列表视图适配器中的一些代码,其中设置了列表视图中的项目: @Override public View getView(final int position, View convertView, ViewGroup parent) { final
@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;
}
在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.settouch(false)的操作代码>,然后再允许用户再次单击项目。谢谢,但是如何调用由listView适配器设置listView的主活动中的布尔值。类ListCell,。。。在适配器类中,则mclick()在主活动中。