Android ScrollView嵌套RecyclerView要做的三件事儿

一、在RecyclerView外层套一层RelativeLayout:

        这样做为什么?解决RecyclerView展示不全的问题。

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

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>

二、禁止RecyclerView的纵向滑动(横向同理):

        这样做为什么?解决RecyclerView滑动无惯性问题,解决滑动显示头尾阻尼问题。

        recyclerView.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;//禁止滑动
            }
        });   

 三、禁止RecyclerView的默认聚焦:

        这样做为什么?解决RelativeLayout处于非顶部,却在加载后处于页面顶部的问题

        recyclerView.setFocusable(false);//关闭默认聚焦

kotlin版的两项配置:

        recyclerView.layoutManager = object :LinearLayoutManager(this){
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        recyclerView.isFocusable = false


文章到这里就结束了。

如果您有不同的见解,或者疑问的话,欢迎到评论区留言。

如果您感觉深海写的不错的话,请给文章点个赞吧!

感谢各位的支持!


版权声明:本文为qq_39731011原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。