当RecyclerView设置的数据存在空白的情况时,需要显示EmptyView时,发现EmptyView不能填充满RecyclerView,这时候设置“fill_parent”或者“match_parent”是不管用的,因为当NestedScrollView没有设置fillVeewport为true时, 里面的元素(比如RecyclerView)会按照wrap_content来计算。
这时候只需要给NestedScrollView设置 android:fillViewport="true"即可解决问题。
<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:orientation="vertical">
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:padding="15dp"
app:text="测试布局"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
另外如果只使用NestedScrollView替换了ScrollView,建议java代码中设置recyclerView.setNestedScrollingEnabled(false);如果不设置那么你的App滑动流畅度就像得了老年痴呆一样,和自定义FullyLinearLayoutManager解决方案的滑动流畅度有的一拼。
网友评论