FragmentPagerAdapter notifyDataSetChanged not working

前端 未结 6 463
天涯浪人
天涯浪人 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 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);
          }
      

提交回复
热议问题