无法正确管理expandableListView

时间:2016-12-28 09:52:31

标签: listview ontouchlistener onitemlongclicklistener

我创建了可扩展列表,并设置了像Slider一样的gesturedetector(每次我在它上面显示和隐藏它)。

expListListener= new GestureDetector.SimpleOnGestureListener(){

    @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                expandableListView.invalidateViews();

                Thread closeSliderThread = new Thread(new closingSlider());
                Thread openingSliderThread = new Thread(new openingSlider());

                    if (e1.getX() - e2.getX() < 0 && Math.abs(e2.getX() - e1.getX()) > difference) {
                       closeSliderThread.start();                   
                       for (int i = 0; i < expandableListView.getCount(); i++)             expandableListView.collapseGroup(i);

                       return true;
                     }


                    if (e2.getX() - e1.getX() > 0
                            && Math.abs(e1.getX() - e2.getX()) > difference)
                    {

                       openingSliderThread.start();                                                              
                       return true;
                    }
                    return false;

            }



            @Override
            public boolean onDown(MotionEvent e) {

                return true;
            }

             class closingSlider implements Runnable{
                 @Override
                 public void run() {
                     rightPartLL.post(new Runnable() {
                         @Override
                         public void run() {
                             synchronized (closingSlider.class) {
                                 rightPartLL.startAnimation(hide_slider);
                             }
                         }
                     });

                 }
             }
            class openingSlider implements Runnable{
                @Override
                public void run() {
                    rightPartLL.post(new Runnable() {
                        @Override
                        public void run() {
                            synchronized (openingSlider.class) {
                                rightPartLL.startAnimation(show_slider);
                            }
                        }
                    });

                }
            }

然后

gestureDetectorOfExpListView = new GestureDetector(activity,expListListener);

这里我告诉expandableList听取我的投掷行为:

expandableListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            return gestureDetectorOfExpListView.onTouchEvent(motionEvent);

        }


    });enter code here

但是当我试图设置一个OnItemLongListener以便在长时间触摸ExpandableList的子项后实现逻辑:

expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                int groupPosition = ExpandableListView.getPackedPositionGroup(id);
                int childPosition = ExpandableListView.getPackedPositionChild(id);

                Toast.makeText(activity,"group: "+groupPosition +" child:"+childPosition,5000).show();

                // Return true as we are handling the event.
                return true;
            }
            return false;
        }
    });
enter code here

......它不起作用。但是当我删除然后我设置的行

OnTouchEvent to expandableListView which return return gestureDetectorOfExpListView.onTouchEvent(motionEvent); 

.... OnItemLongClickListener正常运作!帮助我弄清楚如何实现两种姿势动作和onLongTouches。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

所以我找到了一个优雅的解决方案:

xpandableListView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            return gestureDetectorOfExpListView.onTouchEvent(motionEvent) | expandableListView.onTouchEvent(motionEvent);

        }


    });enter code here