RecyclerView与其他布局视图同步滚动,大部分情况说,全屏滚动;要实现同步滚动,主要实现以下代码:
layout.xml(布局)
<!-- RecyclerView应该是NestedScrollView的子View -->
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<!-- Other views -->
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
UI实现(Kotlin)
// 部分代码
private var _binding:LayoutBinding? = null
private val binding get() = _binding!!
private lateinit var recyclerView: RecyclerView
recyclerView = binding.recyclerView
recyclerView.layoutManager = LinearLayoutManager(requireActivity()) // 需要添加,否则adapter不能展现
recyclerView.isNestedScrollingEnabled = false // 赋值false,实现同步滚动