'how use dynamic item in recycler view with grid layout manager

I have several items with different types that have different views and I use them in the recycler view, but the problem is that in the grid layout manager the height of the item that is smaller than the column item next to it is the same height, but I want to like Staggered layout manager, but it does not have the spanSizeLookup method. What should I do? Can anyone help?

i want this: enter image description here

But this shows: enter image description here

this is my code:

        val layoutManager = GridLayoutManager(
        requireContext(),
        4,
        GridLayoutManager.VERTICAL,
        false
    )

    layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
        override fun getSpanSize(position: Int): Int {
            return if (position == 1 || position == 2) 1 else 2
        }
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source