Android解决RecyclerView中的item显示不全方案

最近的项目中实现订单确定页面。需要使用ScrollView嵌套RecyclerView,当RecyclerView中的item数量比较多时,就会出现item只显示一部分数据,并没有将用户勾选的商品数量全部显示出来,这个时候就需要我们做一下处理了。

下面来说两种解决方案:

1、使用5.0的新控件NestedScrollView替换ScrollView.
NestedScrollView支持嵌套滑动,既能填item显示不全的坑,又可以填嵌套滑动卡顿的坑。不了解的童鞋可以去学习一波,这里就不做详细的说明了。

用法:
(1)、布局文件中将ScrollView替换成"android.support.v4.widget.NestedScrollView".
(2)、使用代码设置recyclerView.setNestedScrollingEnabled(false)即可。

2、在RecyclerView的外面嵌套一层RelativeLayout,然后添加属性 android:descendantFocusability=“blocksDescendants”.

用法参考:

<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:descendantFocusability="blocksDescendants">
     
        <android.support.v7.widget.RecyclerView
             android:id="@+id/recyclerView"
             android:layout_width="match_parent"
             android:layout_height="match_parent"  
             android:overScrollMode="never"/>
             
</RelativeLayout>

说到这我们再来熟悉一下 android:descendantFocusability="blocksDescendants"属性的作用:

该属性的含义是:当一个view获取焦点时,定义ViewGroup和其子控件两者之间的关系。

它一共有3个属性值,它们分别是:

beforeDescendants:viewGroup会优先子类控件而获取焦点;

afterDescendants:viewGroup只有当子类控件不需要获取焦点的时候才去获取焦点;

blocksDescendants:viewGroup会覆盖子类控件而直接获取焦点。

两种方案到这里就介绍完了。


以下是个人公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识及分享个人爱好的文章,有问题可以留言哦:

原文地址:https://www.cnblogs.com/showly/p/11229795.html