How to get List View Item onTouch method?

后端 未结 2 886
眼角桃花
眼角桃花 2020-12-21 05:41

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 pub

相关标签:
2条回答
  • 2020-12-21 06:20

    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.

    0 讨论(0)
  • 2020-12-21 06:31

    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);
           ...
        }
     }
    
    0 讨论(0)
提交回复
热议问题