0

I have two tabs in which I add two different lists of data, both tabs share a single recyclerview, so at my viewpager adapter, I just create a new instance to populate the data

From the View

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()

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()
        }

So, the list is displayed correctly, lets say I have two tabs, first tab has 2 items and second tab has 1 item, so, If I click on item 1 at tab 1 I get its id and get the right product, then if I click item 2 on tab 1 it also works, when I swipe to tab 2 and click item 1 it will display correctly again the item, but, if I go back to tab 1 and click item 2 it will throw a IndexOutOfBoundsException, it seems like when swiping back it takes the latest recyclerview data set

I dont know how to fix this to prevent creating a different fragment for tab 2 since they show the same data

SNM
  • 5,625
  • 9
  • 28
  • 77
  • I need to know what is happening, it seems that the last FragmentProducts.newInstance(drinksList) is replacing the whole recyclerview at tab 1 – SNM Mar 12 '20 at 23:00
  • This code seems good. Can you post the whole file? – GHH Mar 13 '20 at 01:15
  • what you actually need ? @GuanHongHuang here is where I send the data (first chunk of code) then the adapter that handles it, and at last the fragment which inflates it – SNM Mar 13 '20 at 01:15
  • You say that the data is incorrect when go back to tab 1, so I am not sure if you do something when viewPager swipe, maybe fragment is reCreated? – GHH Mar 13 '20 at 01:30
  • the data stays the same when going back to tab 1, the thing is that items reference to tab 2 content and not those items in tab 1 – SNM Mar 13 '20 at 01:38

0 Answers0