FragmentStatePagerAdapter getItem未调用

时间:2016-07-22 23:51:55

标签: java android android-fragments android-viewpager fragment

我试图在viewpager中显示一些片段。 我使用

从NavigationDrawer启动PlanFragment
FragmentTransaction transaction = appCompatActivity.getFragmentManager().beginTransaction();
transaction.replace(R.id.activity_main_frame, navItem.getFragment()).commit();

第一次一切正常,但如果我从NavigationDrawer中选择另一个片段并在之后返回PlanFragment,则viewpager不会显示内容(标签可见)。如果您尝试切换标签 标签的行为变得疯狂,因此指示栏在中间停止并且非常缓慢(指示栏不能正常跳转到下一个标签条目)

我发现FragmentStatePagerAdapter中的getItem()方法第二次没有被调用。 我已经尝试使用getChildFragmentManager(),但这没有用。我也使用API​​级别16,因此无法使用。 我使用android.support.v13进行片段支持。

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>

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>

1 个答案:

答案 0 :(得分:0)

您需要在某处使用setOffScreenPageLimit,以便其他页面可用。默认值为1,因此滑动时无效。