how to trigger proper Longclick event on listview

依然范特西╮ 提交于 2020-01-07 01:18:54

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/26354950/how-to-trigger-proper-longclick-event-on-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!