Android studio - 如何将多个recyclerviews置于活动中并使屏幕可滚动?
Posted
技术标签:
【中文标题】Android studio - 如何将多个recyclerviews置于活动中并使屏幕可滚动?【英文标题】:Android studio - how to put multiple recyclerviews in activity and make the screen scrollable? 【发布时间】:2021-07-17 10:13:49 【问题描述】:我想向活动添加多个回收站视图,但屏幕上没有足够的空间。如何使屏幕可滚动,以便有更多空间来添加回收站视图?
【问题讨论】:
使用子ViewGroup
将它们全部放在一个 NestedScrollView
中。
【参考方案1】:
您的解决方案就在这里
<androidx.core.widget.NestedScrollView
android:layout_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_
android:layout_/>
<androidx.recyclerview.widget.RecyclerView
android:layout_
android:layout_/>
<androidx.recyclerview.widget.RecyclerView
android:layout_
android:layout_/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
注意::根据需要更改回收站视图的高度。 随时询问是否 有些不清楚
【讨论】:
您好,感谢您的回答。我放在那里的两件物品之间有很大的空间。我怎样才能减少它?【参考方案2】:如果您想一个接一个地打开两个 RecyclerView,我强烈建议您只使用一个具有不同 ViewHolders 的视图,具体取决于您要显示的内容。
enum class ContentType(val adapterTypeId: Int)
TYPE_ONE(AdapterTypeId.ADAPTER_TYPE_ONE),
TYPE_TWO(AdapterTypeId.ADAPTER_TYPE_TWO);
companion object
fun getTypeFromAdapterTypeId(adapterId: Int) = when (adapterId)
AdapterTypeId.ADAPTER_TYPE_ONE -> TYPE_ONE
AdapterTypeId.ADAPTER_TYPE_TWO -> TYPE_TWO
else -> throw RuntimeException("Unknown Content type")
private object AdapterTypeId
const val ADAPTER_TYPE_ONE = 0
const val ADAPTER_TYPE_TWO = 1
创建一个密封的类,它将作为所有项目的基类:
sealed class GalleryContent(val contentType: ContentType)
然后在您的适配器中:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
return when (ContentType.getTypeFromAdapterTypeId(viewType))
ContentType.TYPE_ONE -> // Bind proper ViewHolder
ContentType.TYPE_TWO -> // Bind proper ViewHolder
override fun getItemViewType(position: Int): Int
val item = itemList[position]
return item.contentType.adapterTypeId
当然你可以添加任意数量的不同类型
【讨论】:
以上是关于Android studio - 如何将多个recyclerviews置于活动中并使屏幕可滚动?的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio 第六十三期 - Android框架 -RecycleView所有用法
如何在 Android Studio logcat 中过滤多个单词