記錄一個之前遇到的問題:
scrollView 嵌套recycleView 導致recycleView 復用失效問題
布局文件
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_scroll_inner"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@android:color/white" />
</LinearLayout>
</androidx.core.widget.NestedScrollView
解決辦法就是再代碼中手動設(shè)置recycleView的固定高度;具體的高度根據(jù)自己的業(yè)務(wù)設(shè)置;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scroll_view_test_layout1_converttest)
fillRecycleViewHeight()
recycle_view.adapter = adapter
recycle_view.layoutManager = LinearLayoutManager(this)
}
fun fillRecycleViewHeight() {
var recycleHeight = windowManager.defaultDisplay.height - recycle_view.top
recycle_view.layoutParams.height = recycleHeight
}