TabLayout - ViewPager - 片段

时间:2016-06-20 08:04:10

标签: android android-viewpager fragment android-tablayout

我的ViewPager和它包含的片段存在问题。我有2个菜单(A和B),我的主页是直接A.当我点击菜单B片段,其中包含一个TabLayout,其中包含一个ViewPager本身有三个片段(每个片段包含一个带有Lorem ipsum的简单TextView)。 ViewPager的3个片段是正确的但如果我点击菜单A然后再次点击菜单B我没有任何内容。片段1和2上没有任何内容与第3个对象仍然有文本,如果我返回到片段1读取收入(对于片段2没有任何反对意见)。

这是我的代码:

菜单B(FragmentTabLayout)

public class TabLayoutFragment extends Fragment {

    private static final String ARG_TEXT = "ARG_TEXT";
    private static final String ARG_COLOR = "ARG_COLOR";

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

    public static TabLayoutFragment newInstance(String text, int color) {
        Bundle args = new Bundle();
        args.putString(ARG_TEXT, text);
        args.putInt(ARG_COLOR, color);

        TabLayoutFragment tabLayoutFragment = new TabLayoutFragment();
        tabLayoutFragment.setArguments(args);

        return tabLayoutFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        setHasOptionsMenu(true);
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_tab_layout, container, false);

        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        toolbar.setBackgroundColor(getArguments().getInt(ARG_COLOR));
        toolbar.setTitle(getArguments().getString(ARG_TEXT));

        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        setupViewPager(viewPager);


        tabLayout = (TabLayout) view.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setBackgroundColor(getArguments().getInt(ARG_COLOR));
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.tab_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {

        //private final List<TabFragment> mFragmentList = new ArrayList<>();
        //private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }
      
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return TabFragment.newInstance("Fragment 2-1");
                case 1:
                    return TabFragment.newInstance("Fragment 2-2");
                case 2:
                    return TabFragment.newInstance("Fragment 2-3");
                default:
                    return TabFragment.newInstance("Fragment Default");
            }
        }

        @Override
        public int getCount() {
            //return mFragmentList.size();
            return 3;
        }
    }
}

ViewPager中的片段:

public class TabFragment extends Fragment {

    private static final String ARG_TEXT = "ARG_TEXT";

    private TextView tv;

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

    public static TabFragment newInstance(String text) {
        Bundle args = new Bundle();
        args.putString(ARG_TEXT, text);

        TabFragment tabFragment= new TabFragment();
        tabFragment.setArguments(args);

        return tabFragment;
    }

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

        tv = (TextView) view.findViewById(R.id.tv);

        return view;
    }

}

布局FragmentTabLayout:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

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

</android.support.design.widget.CoordinatorLayout>

布局tabFragment:

<LinearLayout 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"
    tools:context="com.application.myapplication.TabFragment">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/lorem_ipsum"
        />

    </android.support.v4.widget.NestedScrollView>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您可以扩展ViewPagerAdapter

FragmentStatePagerAdapter

而不是

FragmentPagerAdapter

我有同样的问题,对我有用。

答案 1 :(得分:0)

在我的意见中,更好的方法是你应该尝试使用childFragmentManager来嵌套片段

以下是一个简单的例子:

private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getChildFragmentManager());
        viewPager.setAdapter(adapter);
    }

希望它能解决您的问题。