sequence concatadapter based on data type

185 Views Asked by At

is it possible to use concatadapter based on data type.

normal implementation of concatadapter is

 val concatadapter = listOf(firstAdapter, secondAdapter, thirdAdapter)

while trying this code,

 for (nums in 0..15) {
      if (nums % 2 == 0) {
           concatAdapter.add(firstAdapter)
      } else {
           concatAdapter.add(secondAdapter)
      }
 }

the first "firstAdapter" and "secondAdapter" will be display. But the next "firstAdapter" and "secondAdapter" is not displayed.

is there anything wrong on this code?

1

There are 1 best solutions below

0
Ravi Sharma On

The reason that other adapter is not visible is because you are adding same object for "firstAdapter" and "secondAdapter" every time in concat adapter.

you have to initialise "firstAdapter" and "secondAdapter" when adding to concat adapter like below:

 concatAdapter=ConcatAdapter()
    for (nums in 0..15) {
        if (nums % 2 == 0) {
            concatAdapter.addAdapter(FirstAdapter(nums))
        } else {
            concatAdapter.addAdapter(SecondAdapter(nums))
        }
    }
    var layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
    binding.recycler.layoutManager = layoutManager
    binding.recycler.adapter=concatAdapter