gpt4 book ai didi

android - 如何制作 Groupie recylerview 2x2 布局

转载 作者:行者123 更新时间:2023-11-29 18:46:43 26 4
gpt4 key购买 nike

我正在使用 groupie recycler View 来显示 4 个项目。我希望布局为 2x2,而不是所有布局都在彼此之下。这是我目前所拥有的及其显示方式:

模块类:

data class Module ( val Code: String,
val Desc: String){
constructor(): this("", "")
}

模块 fragment 代码 fragment :

private fun updateRecyclerView(items: List<Item>) {
fun init() {
recycler_view_mod.apply {
layoutManager = LinearLayoutManager(this@ModuleFragment.context)
adapter = GroupAdapter<ViewHolder>().apply {
moduleSection = Section(items)
add(moduleSection)
//setOnItemClickListener(onItemClick)
}
}
shouldInitRecyclerView = false
}
fun updateItems() = moduleSection.update(items)
if (shouldInitRecyclerView)
init()
else
updateItems()
}

模块项:

class ModuleItem (val module: Module,
val modId: String,
private val context: Context)
: Item(){
override fun bind(viewHolder: ViewHolder, position: Int) {
viewHolder.textView_Code.text = module.Code
viewHolder.textView_Description.text = module.Desc
}

override fun getLayout() = R.layout.item_module

模块 fragment 布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ModuleFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_mod"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

模块项布局:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardView_item_module"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="4dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/white"
android:clickable="true"
android:focusable="true"
app:cardCornerRadius="4dp"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_Code"
android:textColor="@color/colorPrimaryDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/imageView_profile_picture"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:textSize="25sp"
android:textAppearance="@style/Base.TextAppearance.AppCompat"
tools:text="Code" />
<TextView
android:id="@+id/textView_Description"
android:textColor="@color/colorPrimaryDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textView_Code"
android:layout_below="@+id/textView_Code"
android:layout_marginStart="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
tools:text="Description" />
</RelativeLayout>
</android.support.v7.widget.CardView>

截图:

enter image description here

如有任何帮助,我们将不胜感激,如果您需要更多信息,请告诉我

最佳答案

所以你所要做的就是调整layoutmanager,像这样:

private fun updateRecyclerView(items: List<Item>) {
fun init() {
recycler_view_mod.apply {
layoutManager = GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
adapter = GroupAdapter<ViewHolder>().apply {
moduleSection = Section(items)
add(moduleSection)
//setOnItemClickListener(onItemClick)
}
}
shouldInitRecyclerView = false
}
fun updateItems() = moduleSection.update(items)
if (shouldInitRecyclerView)
init()
else
updateItems()
}

关于android - 如何制作 Groupie recylerview 2x2 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51592624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com