0

So, I have a viewpager with two tabs, each tab needs its own set of data, so I have done this in order to send two different lists to the viewpager container, and then place this list in a shared recyclerview which each tab will display

So , this how I send the data to the adapter

 val allProducts = completeProductList
 val productsList = mutableListOf<Products>()
                    val drinksList = mutableListOf<Products>()
                    for (product in allProducts) {
                        if (product.isDrink) {
                            drinksList.add(product)
                        } else {
                            productsList.add(product)
                        }
                    }

                    viewPagerAdapter.productsList = productsList
                    viewPagerAdapter.drinksList = drinksList
                    viewPagerAdapter.notifyDataSetChanged()

Then in my Adapter I populate each list and then if I select one tab I load drinks or load products

Adapter

class PagerAdapter(fragmentActivity: FragmentActivity) :
    FragmentStateAdapter(fragmentActivity) {

    var productsList: MutableList<Product> = arrayListOf()
    var drinksList: MutableList<Product> = arrayListOf()

    override fun getItemCount(): Int {
        return 2
    }

    override fun createFragment(position: Int): Fragment {
        return when(position){
            0 -> {
                FragmentProducts.newInstance(productsList)
            }
            else -> {
                FragmentProducts.newInstance(drinksList)
            }
        }
    }
}

Then in my FragmentProducts

companion object {
        fun newInstance(product: MutableList<Product>) = FragmentProducts().apply {
            arguments = Bundle().apply {
                putParcelableArrayList(ARG_PROD,ArrayList<Parcelable>(product))
            }
        }
    }

    // I get the product list from the adapter, either drinks or normal products
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            arguments?.let {
                productsList = it.getParcelableArrayList<Product>(ARG_PROD)
            }
        }

    // Then I just set it up to the shared recyclerview
     override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            adapter.productsList = productsList!!
            adapter.notifyDataSetChanged()
        }

Now, this is working fine, my products are sent to each tab, they are shown in the RecyclerView, but here is the problem.

The Problem

If I click a product from drinks recyclerview it will show me that product, but if I go to the product tab and press one of the normal products, it will have the data of the drinks product, and if I click another element it will be an indexOutOfBoundsException

The data is placed right in the two instances of FragmentProducts but I suspect that the recyclerview are not unique for each tab and they share the same data

I have an error of indexes whenever I click one or another element in different tabs

EDIT

I think the problem might be because I can access the populated recyclerview of the second instance, so thats why I can only click on drinks elements but not in products elements in my first tab.

SNM
  • 5,625
  • 9
  • 28
  • 77

0 Answers0