通过onTouch来确定点击的是listView哪一个item

其实这主要是用了ListView的一个方法,通过坐标就可以确定当前是哪一个listView,别的我就不多说了直接看看代码吧,

lv_flide.setOnTouchListener(new OnTouchListener() {
float oldX = 0;
float oldY = 0;
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//保存按下时的X,Y坐标
oldX = event.getX();
oldY = event.getY();
break;
case MotionEvent.ACTION_UP:
//获取按起时的X,Y坐标
float newX = event.getX();
float newY = event.getY();
//通过ListView中的pointToPosition方法获取点击ListView中的位置position
final int oldPosition = ((ListView)v).pointToPosition((int)oldX, (int)oldY);
int newPosition = ((ListView)v).pointToPosition((int)newX, (int)newY);

if( newX - oldX > 20  && oldPosition == newPosition) {
//获取ListView中点击是的View
view = ((ListView)v).getChildAt(oldPosition);
for(int n=0;n{
if(n==oldPosition)
{
dataMap.put(n, true);
}
else
{
dataMap.put(n, false);
}
}
index  = oldPosition;
adapter.notifyDataSetChanged();
/*holder.delete.setVisibility(View.VISIBLE);
holder.delete.setTextColor(Color.RED);
//删除ListView指定项
holder.delete.setOnClickListener(new OnClickListener(){


@Override
public void onClick(View arg0) {
removeItemView(view,oldPosition);
}
});*/
// removeItemView(view,oldPosition);
}
break;
default:
break;
}


return false;
}
});

你可能感兴趣的:(通过onTouch来确定点击的是listView哪一个item)