前言
在写一个ScrollView包裹RecyclerView的功能时,要求RecyclerView包裹数据长度且不可滑动,设置不可滑动后发现item只展示了两条数据,删掉不可滑动代码后数据滑动显示则正常。
二、解决步骤
1.xml布局
代码如下(示例):
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/llKeborad"
android:layout_below="@+id/rl_top"
android:fadingEdge="none" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
此处注意最好使用NestedScrollView,并且在RecyclerView的父布局中加入
android:descendantFocusability="blocksDescendants"属性,也就是示例中的LinearLayout中添加
2.java代码示例
代码如下(示例):
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
@Override
public boolean canScrollVertically() {
return false;
}
};
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setNestedScrollingEnabled(false);//禁止滑动
以上代码设置RecyclerView不可滑动,配合xml就可以解决RecyclerView条目显示不全问题(只显示两个item)得问题了
总结
以上就是今天要讲的内容,本文仅仅简单介绍了使用不可滑动RecyclerView条目显示不全问题(只显示两个item)的解决方法。