Android6.0 ScrollView与RecyclerView滑动冲突的问题

前记:我有个这样的需求,在一个LinearLayout中Vertical展示俩个不同的recycleview,但是要求俩个recycleview都全部展示出来;

在Android5.0的系统中,我的做法是,只要在LinearLayout外面加一个ScrollView我的问题就解决了。在Android5.0的机器上运行,效果确实是我要的。代码如下:

    <ScrollView
        android:id="@+id/sv_search_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/size_10"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_video_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_exam_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

但这段代码放到Android6.0系统的机器上面跑,我发现第二个RecyclerView的内容没有铺开展示,第二个RecyclerView可以滑动。而且发现还有个问题,即使ScrollView中只放一个RecyclerView,全部铺开展示,滑动起来的时候有明显的卡顿感。

网上查了资料,说在Android6.0系统中,如果RecyclerView与RecyclerView一起使用,确实存在俩者滑动冲突的问题。滑动冲突需要自己想办法解决。

结合网上的资料,我自己的解决办法是。

1,先解决第一个recyclerview都能完整显示的问题

在俩个recyclerview外面都包裹一个相对布局RelativeLayout,一定是俩个recyclerview都要包裹。我试着只包裹下底部的一个,发现当底部的数据增多时,会出现异常。

代码如下:

    <ScrollView
        android:id="@+id/sv_search_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/size_10"
        android:scrollbars="none">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_video_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.hp.appquestion.view.CustomRecyclerView
                    android:id="@+id/rv_exam_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

上面就解决了Android6.0上俩个recyclerview都能完整显示的问题

2,解决滑动recyclerView时有卡顿的问题

原因还是滑动冲突的问题,我的解决方案是,重写LinearLayoutManager,设置让其不可滑动,外部滑动靠ScrollView,这样就解决了滑动时卡顿的问题
代码如下:

    public class ScrollLinearLayoutManager extends LinearLayoutManager {
        private boolean isScrollEnabled = true;

        public ScrollLinearLayoutManager(Context context) {
            super(context);
        }

        public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        public void setScrollEnabled(boolean flag) {
            this.isScrollEnabled = flag;
        }

        @Override
        public boolean canScrollVertically() {
            return isScrollEnabled && super.canScrollVertically();
        }
    }

使用:

    ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(this);
    scrollLinearLayoutManager.setScrollEnabled(false);
    mRecyclerView.setLayoutManager(scrollLinearLayoutManager);

这样,俩个问题就都解决了!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值