Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager Fragments turning blank

Using a PagerSlidingTabStrip with a Viewpager inside of a fragment:

  • The main Activity contains a "main fragment" that changes depending on what item you click in navigation drawer.
  • When loading the initial fragment containing the Viewpager everything shows up fine (all pages are populated).
  • Replacing that main fragment with another one and then going back to the viewpager fragment turns every page in the viewpager blank, but the PagerSlidingTabStrip tabs are still there.

Any ideas?

like image 971
Marcus Gruneau Avatar asked Mar 26 '15 11:03

Marcus Gruneau


People also ask

Is ViewPager deprecated?

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated. This method should be called by the application if the data backing this adapter has changed and associated views should update.

How do you get ViewPager fragments?

ViewPager save Fragment in FragmentManager with particular tags, So We can get ViewPager's fragment by FragmentManager class by providing tag. And ViewPager provide tag to each fragment by following syntax : Fragment page = getSupportFragmentManager(). findFragmentByTag("android:switcher:" + R.

How to set ViewPager in Android?

To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

How does ViewPager2 work?

So a Viewpager is an android widget that is used to navigate from one page to another page by swiping left or right using the same activity. So when we use Viewpager, we can add different layouts in one activity and this can be done by using the fragments.


1 Answers

I had a problem like that

try this

mPager.setAdapter(new BasePagerAdapter(getChildFragmentManager(), getResources()));

you probably have this

mPager.setAdapter(new BasePagerAdapter(getFragmentManager(), getResources()));

EDIT: and in your BasePagerAdapter extend FragmentStatePagerAdapter

public class BasePagerAdapter extends FragmentStatePagerAdapter {
like image 84
xanexpt Avatar answered Sep 22 '22 10:09

xanexpt