项目中有地方用到HorizontalScrollView嵌套RecyclerView,vivo(7.2版本)和华为(8.1)测试时发现数据显示不完全。排查后发现HorizontalScrollView宽度不超过屏幕,导致数据无法显示完全,也无法水平滑动。
网上查找资料测试后发现修改如下,能实现HorizontalScrollView宽度能超过屏幕包含所有项目并能正常滑动。
在RecycleView外层嵌套一个RelativeLayout里面添加 android:descendantFocusability=”blocksDescendants”这个属性即可。
代码如下:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingLeft="@dimen/public_space_value_10"
android:paddingRight="@dimen/public_space_value_10"
android:scrollbars="none">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</HorizontalScrollView>