简体   繁体   中英

ViewPager2 with Fragment data set change

I'm trying to create tab layout with viewpager2 inside it having fragment and recylerview to show data. While using with Dummy data it is working fine, but when I need to update data in recylerview the data set is not changing. This is what I have tried

for refreshing the data

viewPagerAdapter.refreshOfflineFragAdapter(1,expiryOfflineList,trendingOfflineList)
 fun refreshOnlineFragAdapter(index: Int, expirySoon: ArrayList<OnlineOfferAPIResult>?,trending:ArrayList<OnlineOfferAPIResult>?) {
      fragmentList[index] = OfferFrag.newInstance(expirySoon,trending)
      notifyItemChanged(index)

 }

second try

viewPagerAdapter.refreshOfflineFragAdapter(1,OfferFrag.newInstance(expiryOfflineList,trendingOfflineList))
 fun refreshOfflineFragAdapter(index: Int, fragment: OfferFrag) {
      fragmentList[index] = fragment
      notifyItemChanged(index)
 }

this is the fragment class for viewpager

class OfferFrag:BaseFragment() {

      companion object{
           private val ExpirySoonOffers = "expirySoonoffers"
           private val TrendingOffers = "trendingOffers"

           fun newInstance(expirySoonoffers:ArrayList<OnlineOfferAPIResult>?,trendingOffers:ArrayList<OnlineOfferAPIResult>?): OfferFrag {
                val args = Bundle()
                val fragment = OfferFrag()
                args.putParcelableArrayList(ExpirySoonOffers,expirySoonoffers)
                args.putParcelableArrayList(TrendingOffers,trendingOffers)
                fragment.arguments = args
                return fragment
           }
      }
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
           super.onViewCreated(view, savedInstanceState)
           if(arguments!=null){
                if (this.requireArguments().containsKey(ExpirySoonOffers)) {
                     expiringOfferList = this.requireArguments().getParcelableArrayList<OnlineOfferAPIResult>(ExpirySoonOffers) as ArrayList<OnlineOfferAPIResult>
                }
                if (this.requireArguments().containsKey(TrendingOffers)) {
                     trendingOfferList?.value = this.requireArguments().getParcelableArrayList<OnlineOfferAPIResult>(TrendingOffers) as ArrayList<OnlineOfferAPIResult>
                }
           }
      }

Now when I debugged the code I found that when the newInstance is called again it has values but when onCreateView is called it is taking the arguments data empty. ie trendingOfferList and expiryOfferList are null in onViewCreated .

Please if anyone has any solution for this issue.

Creating a fragment is very expensive operation, if you have UI that you want to update I'd recommend to you notify fragment about UI changes considering that you have reference to the Fragment:

viewPagerAdapter.refreshOfflineFragAdapter(1,expiryOfflineList,trendingOfflineList)
 fun refreshOnlineFragAdapter(index: Int, expirySoon: ArrayList<OnlineOfferAPIResult>?,trending:ArrayList<OnlineOfferAPIResult>?) {
      fragmentList[index].update(expirySoon,trending)

 }

In the fragment just set new data to the UI elements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM