我正在使用Groupie Recycler视图显示4个项目。我希望布局是2x2,而不是全部都在一个下面。这是我到目前为止的内容及其显示方式:
模块类:
data class Module ( val Code: String,
val Desc: String){
constructor(): this("", "")
}
模块片段代码片段:
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()
}
ModuleItem:
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
}
模块片段布局:
<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>
ModuleItem布局:
<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>
ScreenShot:
任何帮助将不胜感激,如果您需要更多信息,请告诉我
答案 0 :(得分:2)
因此,您要做的就是调整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()
}
答案 1 :(得分:0)
您只需要使用正确的布局管理器,就可以达到此效果,只需将跨度设置为2
val mLayoutManager = GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
recyclerView.setLayoutManager(mLayoutManager)