片段中的滑动标签从第二次出现故障

Sha*_*hiq 3 android android-fragments

我有一个MainActivity,它包含一个Sliding抽屉菜单和一个FragmentContainer来切换片段.

我有一个名为History的片段,它有这样的布局

<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>
Run Code Online (Sandbox Code Playgroud)

这堂课看起来像这样

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;
        }
    }


}
Run Code Online (Sandbox Code Playgroud)

从"NavigationSlider"菜单调用时,"历史记录"页面首次正常工作.历史页面包含两个带有两个片段的滑动选项卡.这些是第一次显示,一切正常.

问题发生在第二次或之后.没有显示错误,加载了布局,显示了滑动标签,但没有显示它们的碎片,滑块出现故障.

这个问题可能是什么原因?根据StackOverflow的回答,我尝试使用不同的方法在片段中实现滑块.仍然是同样的问题.

提前致谢.

Cod*_*ury 10

更换

pager.setAdapter(new PagerAdapter(getActivity().getSupportFragmentManager()));
Run Code Online (Sandbox Code Playgroud)

同

pager.setAdapter(new PagerAdapter(getActivity().getChildFragmentmanager()));
Run Code Online (Sandbox Code Playgroud)

原因:

CHILD FragmentManager是处理它添加到的片段中包含的片段的片段.