Search code examples
androidandroid-layoutandroid-listviewonlongclicklistener

how to trigger proper Longclick event on listview


i have to show Android Contextual action mode on long press of list view but when we long press then some multiple event trigger and Contextual menu hide so is there way to handle this problems.

i also try return true on onLongClick() but its not working

thanks in advance for your help


Solution

  • After search on stackoverflow i found my answer using this question implement GestureDetector on my listview an here is my code

    set GestureDetector on listview

    final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector());
            View.OnTouchListener gestureListener = new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event); 
                }};
            mMessageListView.setOnTouchListener(gestureListener);
    

    and this code of MyGestureDetector

    class MyGestureDetector extends SimpleOnGestureListener{ 
    
               @Override
            public void onLongPress(MotionEvent e) {
                super.onLongPress(e);
                ListView lv = mMessageListView;
                int pos = lv.pointToPosition((int)e.getX(), (int)e.getY());
                if (listMsg.get(pos).type==ChatItem.ITEM) {
                    mMessageListView.setItemChecked(pos, !adapter.isPositionChecked(pos));
                } 
            }
    
           }
    

    i share this ans so it can helpful for other share