Why 'Pageradapter::Notifydatasetchanged' Is Not Updating the View

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

There are several ways to achieve this.

The first option is easier, but bit more inefficient.

Override getItemPosition in your PagerAdapter like this:

public int getItemPosition(Object object) {
return POSITION_NONE;
}

This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained.

The second option, suggested by Alvaro Luis Bustamante (previously alvarolb), is to setTag() method in instantiateItem() when instantiating a new view. Then instead of using notifyDataSetChanged(), you can use findViewWithTag() to find the view you want to update.

Conclusion

If you have a lot of views, or want to support modifying any specific item and/or view (fastly at any time), then the second approach (tagging) is very flexible and high performant, as it prevents recreating all the not modified views.

(Kudos to alvarolb for the original research.)

But if your App has only a "refresh" feature (without single item changes being even allowed), or has just few items, use the first approach, as it saves development time.

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

There are several ways to achieve this.

The first option is easier, but bit more inefficient.

Override getItemPosition in your PagerAdapter like this:

public int getItemPosition(Object object) {
return POSITION_NONE;
}

This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained.

The second option, suggested by Alvaro Luis Bustamante (previously alvarolb), is to setTag() method in instantiateItem() when instantiating a new view. Then instead of using notifyDataSetChanged(), you can use findViewWithTag() to find the view you want to update.

Conclusion

If you have a lot of views, or want to support modifying any specific item and/or view (fastly at any time), then the second approach (tagging) is very flexible and high performant, as it prevents recreating all the not modified views.

(Kudos to alvarolb for the original research.)

But if your App has only a "refresh" feature (without single item changes being even allowed), or has just few items, use the first approach, as it saves development time.

notifyDataSetChanged not updating view

You don't show your adapters code, but you need to set the adapters underlying data/list using the it reference before calling notifyDataSetChanged

if (it.isNotEmpty())  {
no_rides.visibility = View.GONE
rideList.adapter!!.data = it // something similar to set the list in your adapter to the new values
rideList.adapter!!.notifyDataSetChanged() //Function called but no visible data
}

PagerAdapter.notifyDataSetChanged does not refresh fragments

I know this question is a bit old, but it might be interesting to know that Google recently solved this problem with ViewPager2. See examples here

First of all, a the following dependency in your build.gradle file :

  dependencies {
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta02'
}

Now you can replace your ViewPager in your xml file with :

    <androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

Then you will need to replace ViewPager by ViewPager2 in your activity

ViewPager2 needs either a RecycleView.Adapter, or a FragmentStateAdapter :

    public class TabAdapter extends FragmentStateAdapter {
private final List<Tab> tabs;

public TabAdapter(@NonNull FragmentManager fm, List<Tab> tabs, Lifecycle lifecycle) {
super(fm, lifecycle);

this.tabs = tabs;
}

@NonNull
@Override
public Fragment createFragment(int position) {
//create your fragment here
}

@Override
public int getItemCount() {
return tabs.size();
}

@Override
public long getItemId(int position) {
// use a distinct id for each item to allow the adapter to see the changes
}
}

In the case you were using a TabLayout, you can use a TabLayoutMediator :

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager, true, new TabLayoutMediator.OnConfigureTabCallback() {
@Override
public void onConfigureTab(@NotNull TabLayout.Tab tab, int position) {
// configure your tab here
tab.setText(tabs.get(position).getTitle());
}
});

tabLayoutMediator.attach();

In order to remove data, after removing the item from your collection you can either call the notifyDataSetChanged() method or more specifically the notifyItemRemoved() with the position of removed item

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

There are several ways to achieve this.

The first option is easier, but bit more inefficient.

Override getItemPosition in your PagerAdapter like this:

public int getItemPosition(Object object) {
return POSITION_NONE;
}

This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained.

The second option, suggested by Alvaro Luis Bustamante (previously alvarolb), is to setTag() method in instantiateItem() when instantiating a new view. Then instead of using notifyDataSetChanged(), you can use findViewWithTag() to find the view you want to update.

Conclusion

If you have a lot of views, or want to support modifying any specific item and/or view (fastly at any time), then the second approach (tagging) is very flexible and high performant, as it prevents recreating all the not modified views.

(Kudos to alvarolb for the original research.)

But if your App has only a "refresh" feature (without single item changes being even allowed), or has just few items, use the first approach, as it saves development time.

FragmentPagerAdapter notifyDataSetChanged not working

I'm not sure about this, but you can try to use FragmentStatePagerAdapter instead of FragmentPagerAdapter . The thing is, i've also run into this issue, and it helped me

kotlin notifyDataSetChanged does not update the view

EDIT 1: Try this

fun setUsers(users : List<User?>) {
usersList?.clear()
usersList?.addAll(users)
}

Where you set recyclerView Adapter:

recyclerView.layoutManager = LinearLayoutManger(this, LinearLayoutManager.Vertical, false)
recyclerView.setAdapter(adapter)

And in your displayRanking function:

private fun displayRanking(users : List<User>) {
visibleLayout.visibility = View.VISIBLE
errorImageView.visibility = View.GONE
progressBar.visibility = View.GONE
val logedInUser = SaveSharedPreference.getLogedInUser(context)
userRank.text = (adapter.usersList?.indexOf(logedInUser)).toString()
userName.text = logedInUser.firstName
userPoints.text = logedInUser.score.toString()

adapter.setUsers(users)
adapter.notifyDataSetChanged()
}

Try calling adapter.notifyDataSetChanged() in your displayRanking function after calling adapter.setUsers(users) and delete notifyDataSetChanged() in your Adapter class.



Related Topics



Leave a reply



Submit