求助:纵向RecyclerView滚动时横向RecyclerView固定顶部无法随滚隐藏
解决横向RecyclerView随纵向滚动隐藏的问题
方案一:将横向RecyclerView作为纵向RecyclerView的Header(推荐)
这种方式贴合RecyclerView的设计逻辑,能避免嵌套滚动带来的性能损耗:
- 修改纵向RecyclerView的Adapter,新增Header类型的列表项
- 在
getItemViewType方法中区分Header和普通内容项 - 在
onCreateViewHolder中创建Header的ViewHolder,加载横向RecyclerView的布局 - 在
onBindViewHolder中为Header内的横向RecyclerView配置Adapter与数据
示例代码片段:
// 定义列表项类型常量 private const val TYPE_HEADER = 0 private const val TYPE_ITEM = 1 override fun getItemViewType(position: Int): Int { return if (position == 0) TYPE_HEADER else TYPE_ITEM } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return if (viewType == TYPE_HEADER) { val headerView = LayoutInflater.from(parent.context).inflate(R.layout.item_header, parent, false) HeaderViewHolder(headerView) } else { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_video, parent, false) ItemViewHolder(itemView) } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { if (holder is HeaderViewHolder) { // 初始化横向RecyclerView holder.categoryChipRv.layoutManager = LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false) holder.categoryChipRv.adapter = yourHorizontalAdapter } else if (holder is ItemViewHolder) { // 绑定普通列表项数据 holder.bindData(dataList[position - 1]) } } // Header对应的ViewHolder类 class HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val categoryChipRv: RecyclerView = itemView.findViewById(R.id.categoryChipRv) }
对应的Header布局item_header.xml:
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/categoryChipRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:paddingStart="12dp" android:paddingEnd="12dp" />
方案二:用LinearLayout包裹两个RecyclerView,外层嵌套NestedScrollView
如果不想修改Adapter,可采用此方式,但需注意处理滚动冲突:
- 用纵向LinearLayout包裹两个RecyclerView,外层套NestedScrollView
- 给两个RecyclerView添加
android:nestedScrollingEnabled="false"属性,避免滚动冲突
修改后的布局代码:
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="90dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/categoryChipRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:nestedScrollingEnabled="false" android:paddingStart="12dp" android:paddingEnd="12dp" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/videoListRv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:clipToPadding="false" android:nestedScrollingEnabled="false" android:paddingStart="12dp" android:paddingEnd="12dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView>
注意:方案二会让纵向RecyclerView的视图复用机制失效,数据量大时性能不如方案一,因此优先推荐方案一。
内容的提问来源于stack exchange,提问作者Shahram Zarie
相关产品推荐
相关产品推荐

