Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setOnTouchListener for an item in a ListView

I want to get an item from my ListView and setOnTouchListener for that, the following is my code but I am unable to set it properly.

listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View selectedView,
                    int position, long id) {

            Log.d("ON ITEM CLICK...", " ON ITEM CLICK... "+ position);

            listView.getChildAt(position).setOnTouchListener(HorizontalListActivity.this);

            ViewHolder viewHolder =(ViewHolder)selectedView.getTag();

            viewHolder.ivThumbnail.setOnTouchListener(HorizontalListActivity.this);

            }
        });

The reason I wanted to access the item is that I want to drag it on the screen.

Is there a better way to do what I want?

like image 571
Arif Nadeem Avatar asked Jan 21 '26 22:01

Arif Nadeem


1 Answers

I'm in the same situation, and have found something that might work, yet I have not had the time to try it out. The basic idea is to dispatch the touch event to the child, like you did, with getChildAt. You listen for touch events in the listview, and simply if you touched the view you are interested in, dispatch the touch event to it to handle it:

 MyControl control = ListViews.getChildAt(position));
 control.onTouchEvent(touchEvent);
like image 63
Beraxa Ádám Avatar answered Jan 23 '26 11:01

Beraxa Ádám



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!