Проблема в том, что элементы отображаются сверху вниз. нижний мод, и мне нужно, чтобы они отображались слева направо.
Это то, что мне нужно:
Вот что у меня получилось:

Как видите, цифры увеличиваются от строки к строке, а не столбец за столбцом .
Вот мой код для LayoutManager:
package com.digiteq.digiteqtask.presentation.utils
import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
internal class CustomLayoutManager(
context: Context,
rows: Int,
private val columns: Int,
isReversed: Boolean = false
) : GridLayoutManager(context, rows, HORIZONTAL, isReversed) {
/* Setting LayoutParams for the child views of the recycler view.*/
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateDefaultLayoutParams())
}
override fun generateLayoutParams(
c: Context?,
attrs: AttributeSet?
): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateLayoutParams(c, attrs))
}
override fun generateLayoutParams(lp: ViewGroup.LayoutParams?): RecyclerView.LayoutParams {
return spanLayoutSize(super.generateLayoutParams(lp))
}
/* The function has been made for Horizontal Recycler View.
* 1. It checks for the orientation to be HORIZONTAL.
* 2. Its spancount has already been set to 2 in this case throught the constuctor at the initialisation time.
* i.e, layoutManager = new AbsolutefitLayourManager(this,2,GridLayoutManager.HORIZONTAL,false);
* the spancount = 2 , specifies the no. of rows for HORIZONTAL orientation
* 3. The rest of the function divides the horizontal screen width by 2 (spanColumnCount = 2 HERE) hence specyfing the column
width of each view and hence specifying 2 columns (can be made 3, by dividing by three.)
*/
private fun spanLayoutSize(layoutParams: RecyclerView.LayoutParams): RecyclerView.LayoutParams {
if (orientation == HORIZONTAL) {
layoutParams.width = Math.round((getHorizontalSpace() / columns).toDouble()).toInt()
// its the margin between the items
layoutParams.setMargins(2, 2, 2, 2)
}
return layoutParams
}
override fun checkLayoutParams(lp: RecyclerView.LayoutParams?): Boolean {
return super.checkLayoutParams(lp)
}
private fun getHorizontalSpace(): Int {
return width - paddingRight - paddingLeft
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... outmanager