FragmentPagerAdapter notifyDataSetChanged not working

前端 未结 6 461
天涯浪人
天涯浪人 2021-01-31 15:30

I got a FragmentPagerAdapter. It\'s getItem method can return a fragment according to data it has from the outside. After I update the data its suppose to display I

相关标签:
6条回答
  • 2021-01-31 15:56

    For those who still facing refresh issue after trying above methods :

    invalidate viewPager then call notifyDatatSetChanged()

    0 讨论(0)
  • 2021-01-31 15:57

    Every answer here did not work perfectly for me but combining multiple answers into one is right choice.

    public final class SomeFragment extends FragmentStatePagerAdapter{
        private final List<Fragment> fragments;
        private Fragment removeFragment; //Fragment to be removed in the next notifydatasetchanged
    
    
        @Override
        public int getItemPosition(final Object object) {
           return object.equals(this.removeFragment) ? FragmentStatePagerAdapter.POSITION_NONE : this.fragments.indexOf(object);
        }
    
    }
    

    Return POSITION_NONE only for the fragment you would like to remove from the viewpager. Every other fragment is still in your fragment list. Do not forget to remove the "removeFragment" from the list before calling notifyDataSetChanged();

    0 讨论(0)
  • 2021-01-31 15:59

    What Nik Myers is saying is correct. However there is a piece missing. When notifyDataSetChanged is called, the method getItemPosition is called. You need to override this to get the fragments to reload.

     @Override
        public int getItemPosition(Object object) {
            // Causes adapter to reload all Fragments when
            // notifyDataSetChanged is called
            return POSITION_NONE;
        }
    
    0 讨论(0)
  • 2021-01-31 15:59

    none of the solutions given above works for me. So hope below solution may be helpful for you.

    In my code, I have 2 Fragments in viewPager. One is used for 3 EditTexts and 1 button and another fragment has RecyclerView. On button click, my value is going to recyclerView, but it's not updating. So now, I used the below code:

    I called the notifyDataSetChanged() on button click. My MainActivity has inner adapter class extends FragmentStatePagerAdapter class. So i called it using mainActivity.mMainAdapter.notifyDataSetChanged(). Hope it helps someone.

    0 讨论(0)
  • 2021-01-31 16:01

    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

    0 讨论(0)
  • 2021-01-31 16:02

    I have faced the similar problem when I was working on my last project. So I found few Solution.

    1. by overriding getItemPosition in the pager adaptor but this is not a good idea.

      @Override
      public int getItemPosition(Object object) {
          // it will recreate all Fragments when
          // notifyDataSetChanged is called
          return POSITION_NONE;
      }
      
    2. The second one is cabezas Solution.

    3. Most Stable solution override getItemPosition in below fashion:

          @Override
          public int getItemPosition(Object object) {
              if (object instanceof MyFragment) {
                  // Create a new method notifyUpdate() in your fragment 
                  // it will get call when you invoke 
                  // notifyDatasetChaged();
                  ((MyFragment) object).notifyUpdate();
              }
              //don't return POSITION_NONE, avoid fragment recreation.
              return super.getItemPosition(object);
          }
      
    0 讨论(0)
提交回复
热议问题