解决srcollview嵌套recycleview冲突

来源:互联网 发布:剑雨江湖进阶数据2017 编辑:程序博客网 时间:2024/05/16 17:19

正常情况下NestedScrollView嵌套RecycleView,一是会出现只显示一行的情况,二是滑动异常即事件冲突。

解决方法:

1.也是最简单的方法,把design库和V7库升级到23.2以上,注意加上以下代码

 

?
1
2
3
4
5
mLinearLayoutManager.setSmoothScrollbarEnabled(true);
       mLinearLayoutManager.setAutoMeasureEnabled(true);
       cardslist_view.setLayoutManager(mLinearLayoutManager);
       cardslist_view.setHasFixedSize(true);
       cardslist_view.setNestedScrollingEnabled(false);
问题即可解决。

 

2.麻烦一点,重写LinearLayoutManager和NestedScrollView。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
publicclass CustomLinearLayoutManager extendsLinearLayoutManager {
 
    publicCustomLinearLayoutManager(Context context, intorientation, booleanreverseLayout) {
        super(context, orientation, reverseLayout);
    }
 
    privateint[] mMeasuredDimension = newint[2];
 
    @Override
    publicvoid onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                          intwidthSpec, intheightSpec) {
        finalint widthMode = View.MeasureSpec.getMode(widthSpec);
        finalint heightMode = View.MeasureSpec.getMode(heightSpec);
        finalint widthSize = View.MeasureSpec.getSize(widthSpec);
        finalint heightSize = View.MeasureSpec.getSize(heightSpec);
        intwidth = 0;
        intheight = 0;
        for(inti = 0; i < getItemCount(); i++) {
            if(getOrientation() == HORIZONTAL) {
                measureScrapChild(recycler, i,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        heightSpec,
                        mMeasuredDimension);
 
                width = width + mMeasuredDimension[0];
                if(i == 0) {
                    height = mMeasuredDimension[1];
                }
            }else{
                measureScrapChild(recycler, i,
                        widthSpec,
                        View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
                        mMeasuredDimension);
                height = height + mMeasuredDimension[1];
                if(i == 0) {
                    width = mMeasuredDimension[0];
                }
            }
        }
        switch(widthMode) {
            caseView.MeasureSpec.EXACTLY:
                width = widthSize;
            caseView.MeasureSpec.AT_MOST:
            caseView.MeasureSpec.UNSPECIFIED:
        }
 
        switch(heightMode) {
            caseView.MeasureSpec.EXACTLY:
                height = heightSize;
            caseView.MeasureSpec.AT_MOST:
            caseView.MeasureSpec.UNSPECIFIED:
        }
 
        setMeasuredDimension(width, height);
    }
 
    privatevoid measureScrapChild(RecyclerView.Recycler recycler, intposition, intwidthSpec,
                                   intheightSpec, int[] measuredDimension) {
        View view = recycler.getViewForPosition(position);
        recycler.bindViewToPosition(view, position);
        if(view != null) {
            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
            intchildWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                    getPaddingLeft() + getPaddingRight(), p.width);
            intchildHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                    getPaddingTop() + getPaddingBottom(), p.height);
            view.measure(childWidthSpec, childHeightSpec);
            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
            recycler.recycleView(view);
        }
    }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
publicclass MyNestedScrollView extendsNestedScrollView {
    privateint downX;
    privateint downY;
    privateint mTouchSlop;
 
    publicMyNestedScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
 
    publicMyNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
 
    publicMyNestedScrollView(Context context, AttributeSet attrs, intdefStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
 
    @Override
    publicboolean onInterceptTouchEvent(MotionEvent e) {
        intaction = e.getAction();
        switch(action) {
            caseMotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();
                downY = (int) e.getRawY();
                break;
            caseMotionEvent.ACTION_MOVE:
                intmoveY = (int) e.getRawY();
                if(Math.abs(moveY - downY) > mTouchSlop) {
                    returntrue;
                }
        }
        returnsuper.onInterceptTouchEvent(e);
    }
}
阅读全文
0 1