Split a single column list on multiple columns in recyclerView

  Kiến thức lập trình

I want to display a large list of words on a recycler view. Taking advantage of the screen width as compared to the column width, I thought it was a good idea to spread the list on say four columns while keeping the vertical scrolling capability. For this I found this very useful kotlin method chunked :

fun <T> Iterable<T>.chunked(size: Int): List<List<T>>(source)

So in my function setInfoBase() I get a list of entities Mot from a room database query, which I map in a list of String (Mot.mot) that I chunk (size 4) before sending it as the list of data to my adapter:

private var queryResult : List<List<String>> = listOf(listOf())
private fun setInfoBase() {
    val viewModel = ViewModelProvider(requireActivity())[SharedViewModel::class.java]
    viewModel.getMotsFiltres().observe(viewLifecycleOwner) { mots ->
        mots?.let {
            binding.totalBase.text = mots.count().toString()
            binding.totalActifs.text = mots.filter { it.baseActive }.count().toString()
            queryResult = mots.map {it.mot}.chunked(4)

This works like charm.

Then I have an adapter with a list item made of one row of four TextViews (row_mot1...row_mot4) I intent to populate with each line of the chunked queryResult

Here is the simple_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_weight="1" />

        android:layout_weight="1" />

        android:layout_weight="1" />

        android:layout_weight="1" />

The issue is in the adapter, in the override fun onBindViewHolder it seems the lines of the list are truncated to two elements instead of four. The IDE seems to know that, as there is a warning that the line count cannot exceed 2 (why 2?). But I could not find where the issue comes from.

Here is the code of the adapter :

package com.simonwintz.millemots

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class SimpleAdapter (private val motItemLayout: Int) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {
    private var motList : List<List<String>>? = null

    override fun onBindViewHolder(holder: ViewHolder, listPosition: Int) {
        val rowmot1 = holder.rowmot1
        val rowmot2 = holder.rowmot2
        val rowmot3 = holder.rowmot3
        val rowmot4 = holder.rowmot4

        motList?.let {
            val ligne = motList!![listPosition]
            val count = ligne.count()
            rowmot1.text = ligne[0]
            if (count > 1) {
                rowmot2.text = ligne[1]
            } else if (count > 2) {
                rowmot3.text = ligne[2]
            } else if (count > 3) {
                rowmot4.text = ligne[3]

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(motItemLayout, parent, false)
        return ViewHolder(view)

    fun setList(mots: List<List<String>>) {
        motList = mots

    override fun getItemCount(): Int {
        return if (motList == null) 0 else motList!!.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var rowmot1: TextView = itemView.findViewById(R.id.row_mot1)
        var rowmot2: TextView = itemView.findViewById(R.id.row_mot2)
        var rowmot3: TextView = itemView.findViewById(R.id.row_mot3)
        var rowmot4: TextView = itemView.findViewById(R.id.row_mot4)

And here the adapter initialization routine:

private var queryResult : List<List<String>> = listOf(listOf())
private lateinit var adapter : SimpleAdapter
private fun setAdapter() {
    val recyclerView = binding.listeMots
    adapter = SimpleAdapter(R.layout.simple_list_item)
    recyclerView.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
    recyclerView.adapter = adapter

I printed the truncated list queryResult on the logcat at all possible stages and it looks very normal. But I still cannot get rid of the warning and at running the list appears truncated indeed.

Has someone an idea what I am doing wrong ? Thanks for your help !

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website