Android ScrollView嵌套RecyclerView&出现item显示不全的问题

欢迎大家和我分享
欢迎转载,但请保留文章原始出处:
哈哩波特大:
https://www.jianshu.com/u/b7d80b891802

\color{#4285f4}{G}\color{#ea4335}{o}\color{#fbbc05}{o}\color{#4285f4}{g}\color{#34a853}{l}\color{#ea4335}{e}
res:layout

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

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/dp_15">
                </android.support.v7.widget.RecyclerView>
</RelativeLayout>

Java:activity

        //解决数据加载不完的问题
        recycler_view.setNestedScrollingEnabled(false);
        //当知道Adapter内Item的改变不会影响RecyclerView宽高的时候,可以设置为true让RecyclerView避免重新计算大小
        recycler_view.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        recycler_view.setFocusable(false);

方案一

嵌套一层RelativeLayout添加属性 android:descendantFocusability="blocksDescendants"
首先该属性android:descendantFocusability的含义是:当一个view获取焦点时,定义ViewGroup和其子控件两者之间的关系。
它一共有3个属性值,它们分别是:
beforeDescendants:viewGroup会优先子类控件而获取焦点
afterDescendants:viewGroup只有当子类控件不需要获取焦点的时候才去获取焦点
blocksDescendants:viewGroup会覆盖子类控件而直接获取焦点

方案二

首先在xml布局中将你的ScrollView替换成android.support.v4.widget.NestedScrollView,并在java代码中设置recyclerView.setNestedScrollingEnabled(false);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。