Reputation: 745
I'm trying to show some fragments in a viewpager. I start the PlanFragment from a NavigationDrawer using
FragmentTransaction transaction = appCompatActivity.getFragmentManager().beginTransaction();
transaction.replace(R.id.activity_main_frame, navItem.getFragment()).commit();
At the first time everything works fine, but if I select another fragment from the NavigationDrawer and go back to the PlanFragment afterwards the viewpager shows no content (the tabs are visible). If you try to switch the tab the behavior of the tabs becomes crazy so that the indicationbar stops in the mid and it is very sluggish (the indicationbar isn't jumping to the next tab entry as normal)
I found out that the getItem()
method in FragmentStatePagerAdapter isn't called on the second time.
I already tried to use getChildFragmentManager(), but this didn't work. I also uses API Level 16, so this isn't possible to use.
I'm using android.support.v13 for the Fragment Support.
public class PlanFragment extends Fragment {
Toolbar toolbar;
ViewPager viewPager;
PagerAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_vplan, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().setElevation(0);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.removeAllTabs();
tabLayout.addTab(tabLayout.newTab().setText("MONDAY"));
tabLayout.addTab(tabLayout.newTab().setText("TUESDAY"));
adapter = new PagerAdapter(getFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
public class PagerAdapter extends FragmentStatePagerAdapter {
int tabCount;
public PagerAdapter(FragmentManager fm, int tabcount) {
super(fm);
this.tabCount = tabcount;
}
@Override
public Fragment getItem(int position) {
Log.i("ERROR", "YO");
return new TestFragment();
}
@Override
public int getCount() {
return tabCount;
}
}
}
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
The fragment showing in the viewpager
public class TestFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
}
TestFragment XML:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nulla quis lorem ut libero malesuada feugiat. Pellentesque in ipsum id orci porta dapibus. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Vivamus suscipit tortor eget felis porttitor volutpat." />
</android.support.v4.widget.NestedScrollView>
Upvotes: 0
Views: 655
Reputation: 522
You need a setOffScreenPageLimit somewhere so that the other pages are available. The default is 1 so nothing available when you swipe.
Upvotes: 1