How to get List View Item onTouch method?
I have a ListView and I want to perform Drag and Drop on list items. I am Overridding a onTouch
method which has two parameters
@Override public boolean onTouch(View view, MotionEvent me) {}
In view I am getting complete ListView. How can i get perticular TextView on which key is pressed?
I am able to drag item开发者_StackOverflows when if I long press and get that view but i dont want to perform long press action.
Any way to get selected item position in onTouch
?
One way is to implement a custom Adapter
which you use to populate your ListView
. In the getView
method of your Adapter
you can call setOnClickListener
on the view that you create, and add an item click listener that way.
There is some sample code of this in setOnClickListener of a ListView not working.
I have added item position in llItem
tag
in Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
...
llItem = (LinearLayout) rowView.findViewById(R.id.lItem);
llItem.setTag("" + position);
llItem.setOnTouchListener(itemTouch);
...
}
and the extracted item position from tag
OnTouchListener itemTouch = new OnTouchListener() {
private int position;
@Override
public boolean onTouch(View v, MotionEvent event) {
LinearLayout ll = (LinearLayout)v.findViewById(R.id.lItem);
String itemTag = ll.getTag().toString();
int itemPosition = Integer.parseInt(itemTag);
...
}
}
精彩评论