-1

i don't know how to describe it, it like.

enter image description here

I really appreciate your help and have a nice day!

1 Answers1

0

You can refer this for dynamic span count.

Implementation is taken from a Fragment.

https://gist.github.com/mtsahakis/33085460b9707fdf0729

 private var mRecyclerView: RecyclerView? = null

@Nullable
fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view: View = inflater.inflate(R.layout.fragment_view, container, false)
    mRecyclerView = view.findViewById(R.id.recycler_view)
    // ommiting other recycler view set up, such as adapter and Layout manager set up ..
    val viewTreeObserver: ViewTreeObserver = mRecyclerView!!.viewTreeObserver
    viewTreeObserver.addOnGlobalLayoutListener {
        calculateSize() }
}

private val sColumnWidth = 120 // assume cell width of 120dp

private fun calculateSize() {
    val spanCount =
        Math.floor((mRecyclerView!!.width / convertDPToPixels(sColumnWidth)).toDouble())
            .toInt()
    (mRecyclerView!!.layoutManager as GridLayoutManager?)!!.spanCount =
        spanCount
}

private fun convertDPToPixels(dp: Int): Float {
    val metrics = DisplayMetrics()
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics)
    val logicalDensity: Float = metrics.density
    return dp * logicalDensity
}