Sliding tab with fragment violations from the second half

I have a MainActivity that contains a sliding drawer for the menu and a FragmentContainer for switching fragments.

I have a snippet called history that has a layout like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.astuetz.PagerSlidingTabStrip
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimary"
        android:textColor="#FFFFFF"
        app:pstsIndicatorColor="#FFFFFF" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tabs" />

</RelativeLayout>

      

And the class looks like this

public class HistoryFragment extends Fragment {

    public HistoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_history, container, false);


        // Initialize the ViewPager and set an adapter
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        pager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));

        // Bind the tabs to the ViewPager
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        tabs.setViewPager(pager);

        return view;
    }

    class PagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {"Last Transaction", "History"};

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new LastTransaction();
                case 1:
                    return new AboutFragment();
            }

            return null;
        }
    }


}

      

This story page works fine the first time it is called from the NavigationSlider menu. The story page contains two sliding tabs with two snippets. They are displayed for the first time and everything works fine.

The problem arises when they are called a second time or thereafter. No error is displayed, the layout is loaded, sliding tabs are displayed, but their fragments are not displayed and the sliders do not work.

What is the cause of this problem? I tried a different approach to implement sliders in Fragments as per this stackoverflow question . Still the same problem.

Thanks in advance.

+3


source to share


1 answer


replace

pager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));

      

through



pager.setAdapter(new PagerAdapter(getActivity().getChildFragmentmanager()));

      

Cause:

CHILD FragmentManager is the one that handles the fragments contained in the fragment to which it was added.

+10


source







All Articles