zoukankan      html  css  js  c++  java
  • PullToRefreshScrollView嵌套SwipeMenuListView冲突问题解决

    (一)listView内容不正常显示(注:网上有很多方法)

    重写ListView的onMeasure方法

      /**
         * 以下要重写,防止listview在scrollview容器中不能正常显示的问题
         * @param widthMeasureSpec
         * @param heightMeasureSpec
         */
        @Override
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
    
            super.onMeasure(widthMeasureSpec, expandSpec);
    
        }

    (二)ScrollView的上下事件与ListView的左右事件冲突

    重写ListView

    public class MySwipeMenuListView extends SwipeMenuListView {
    
        public MySwipeMenuListView(Context context) {
            super(context);
            mGestureDetector = new GestureDetector(context,onGestureListener);
        }
    
        public MySwipeMenuListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mGestureDetector = new GestureDetector(context,onGestureListener);
        }
    
        public MySwipeMenuListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mGestureDetector = new GestureDetector(context,onGestureListener);
        }
    
    
    //    @Override
    //    public boolean onInterceptTouchEvent(MotionEvent ev) {
    //        switch (ev.getAction()) {
    //            case MotionEvent.ACTION_DOWN:
    //
    //                //setParentScrollAble(false);//当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview 停住不能滚动
    //                Log.w("MyLog", "onInterceptTouchEvent down");
    //            case MotionEvent.ACTION_MOVE:
    //
    //                Log.w("MyLog", "onInterceptTouchEvent move");
    //                break;
    //
    //            case MotionEvent.ACTION_UP:
    //                Log.w("MyLog", "onInterceptTouchEvent up");
    //
    //            case MotionEvent.ACTION_CANCEL:
    //
    //                Log.w("MyLog", "onInterceptTouchEvent cancel");
    //                //setParentScrollAble(true);//当手指松开时,让父ScrollView重新拿到onTouch权限
    //
    //                break;
    //            default:
    //                break;
    //
    //        }
    //
    //        return super.onInterceptTouchEvent(ev) ;
    //    }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            boolean b =  mGestureDetector.onTouchEvent(ev);
            Log.w("MyLog","-- "+ b+" --");
            return super.onTouchEvent(ev);
        }
    
        private GestureDetector mGestureDetector;
        View.OnTouchListener mGestureListener;
    
        private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){
    //        @Override
    //        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    //            Log.w("MyLog","onFling");
    //            float x = e2.getX() - e1.getX();
    //            float y = e2.getY() - e1.getY();
    //            if (Math.abs(y) >= Math.abs(x)){
    //                setParentScrollAble(false);
    //                return true;
    //            }
    //            //当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview 停住不能滚动
    //            setParentScrollAble(true);
    //            return false;
    //        }
    
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.w("MyLog", "onScroll");
                if (distanceY != 0 && distanceX != 0) {
                }
                if (Math.abs(distanceY) >= Math.abs(distanceX)) {
                    return true;
                }
                //当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview 停住不能滚动
                setParentScrollAble(false);
                return false;
            }
        };
    
        /**
         * 是否把滚动事件交给父scrollview
         *
         * @param flag
         */
        private void setParentScrollAble(boolean flag) {
            //这里的parentScrollView就是listview外面的那个scrollview
            Log.w("MyLog", "setParentScrollAble -- " + flag);
            getParent().requestDisallowInterceptTouchEvent(!flag);
        }
    }

    附录:

     <com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:ptr="http://schemas.android.com/apk/res-auto"
            android:id="@+id/pull_refresh_scrollview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            ptr:ptrAnimationStyle="flip"
            ptr:ptrMode="both">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <com.inetgoes.fangdd.view.MySwipeMenuListView
                    android:id="@android:id/list"
                    android:scrollbars="none"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
            </LinearLayout>
    
        </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
  • 相关阅读:
    Node 修改默认镜像源
    Mac下apache虚拟主机配置
    Grep命令出现 Binary file (standard input) matches
    idea取出方法参数提示
    Java8 Optional用法
    Codeforces Round #638 (Div. 2)
    Codeforces Round #637 (Div. 2)
    Codeforces Round #636 (Div. 3)
    Tree
    Educational Codeforces Round 85
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7371692.html
Copyright © 2011-2022 走看看