3

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 items 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?

Harshad
  • 7,904
  • 3
  • 24
  • 42
  • Hey i also face same problem can any one known how to get the Position of List Item when we implement setOnTouchListener for ListView in ListActivity. – Herry Jul 20 '11 at 13:44
  • [Check this answer](http://stackoverflow.com/questions/2909311/android-list-view-drag-and-drop-sort). – Flo Jul 05 '11 at 07:32
  • @Flo.. Its a bit complicated code. :( – Harshad Jul 05 '11 at 12:04
  • I guess you can find your answer here - http://stackoverflow.com/questions/17384131/get-item-from-listview-only-with-ontouchlistener – Shashank Gupta Mar 30 '17 at 16:36

2 Answers2

4

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.

Community
  • 1
  • 1
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • I think for drag'n'drop it would be better to implement an OnTouchListener instead of an OnClickListener. – Flo Jul 05 '11 at 07:32
  • @Flo good point! I missed that it was for dran and drop rather than simple click handling when ansering. @Harshad there is some useful refrence code for drag and drop in http://stackoverflow.com/questions/2909311/android-list-view-drag-and-drop-sort – Mark Allison Jul 05 '11 at 07:38
  • My app has a ViewFlipper (that implements OnGestureListener) assigned to a ListView item and I noticed that the OnGestureListener was preventing the Adapter's OnItemClickListener from triggering onItemClick(). The solution was to assign a `setOnClickListener` to that specific item in the ListView. Now the custom compound view still triggers `onSingleTapUp` followed by the OnClickListener's `onClick()` – Someone Somewhere Jul 01 '13 at 20:35
3

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);
       ...
    }
 }
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
Vitaliy
  • 69
  • 4