Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – ViewPager2 with Fragment data set change

Posted on November 11, 2022 By

Solution 1 :

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

Problem :

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. i.e trendingOfferList and expiryOfferList are null in onViewCreated.

Please if anyone has any solution for this issue.

READ  [FIXED] android - Check If String Contains any Of the Word From Given List
Powered by Inline Related Posts
Android Tags:android, android-fragments, android-recyclerview, android-viewpager, kotlin

Post navigation

Previous Post: [FIXED] android – How to detect when third party app’s dialog box appears in React native?
Next Post: [FIXED] android – Get the list of user SIM cards and select it – flutter

Related Posts

[FIXED] Request json android for Volley Android
[FIXED] android – custom dialog fragment from a fragment Android
[FIXED] view – Which Framework can be used to draw a complex, interactive UI hierarchy on Android? (Like CoreAnimation on iOS) Android
[FIXED] xamarin.forms – How to remove ripple effect from Collection view item in Xamarin Forms? Android
[FIXED] How to add jpg files in drawable folder with different densities in android studio 3.5? Android
[FIXED] android – Extract String in Data Class Kotlin Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme