Update data in ListFragment as part of ViewPager

后端 未结 10 1993
一整个雨季
一整个雨季 2020-11-22 07:55

I\'m using the v4 compatibility ViewPager in Android. My FragmentActivity has a bunch of data which is to be displayed in different ways on different pages in my ViewPager.

10条回答
  •  故里飘歌
    2020-11-22 08:27

    OK, I think I've found a way to perform request b) in my own question so I'll share for others' benefit. The tag of fragments inside a ViewPager is in the form "android:switcher:VIEWPAGER_ID:INDEX", where VIEWPAGER_ID is the R.id.viewpager in XML layout, and INDEX is the position in the viewpager. So if the position is known (eg 0), I can perform in updateFragments():

          HomeListFragment fragment = 
              (HomeListFragment) getSupportFragmentManager().findFragmentByTag(
                           "android:switcher:"+R.id.viewpager+":0");
          if(fragment != null)  // could be null if not instantiated yet
          {
             if(fragment.getView() != null) 
             {
                // no need to call if fragment's onDestroyView() 
                //has since been called.
                fragment.updateDisplay(); // do what updates are required
             }
          }
    

    I've no idea if this is a valid way of doing it, but it'll do until something better is suggested.

提交回复
热议问题