ViewPager在Fragment内有Fragments

时间:2018-07-07 12:25:42

标签: java android android-fragments android-viewpager

我的应用程序中有些障碍,其中包含一个包含ViewPager的片段。与适配器链接的ViewPager也包含一组片段。在我的应用程序中,HomeFragment是基本片段。当我运行应用程序时,一切正常,但片段未加载,仅显示ViewPager的标签,因此出现问题。 在这里,我共享基本片段,PageAdapter和fragment_home.xml文件的代码。

这是我的父片段代码: HomeFragment.java

public class HomeFragment extends Fragment {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    PageAdapter pageAdapter;
    TabItem tabChats;
    TabItem tabStatus;
    TabItem tabCalls;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        View root= inflater.inflate(R.layout.fragment_home, container, false);

        toolbar = (Toolbar)root.findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.app_name));
        //setSupportActionBar(toolbar);
        tabLayout = (TabLayout)root.findViewById(R.id.tablayout);
        tabChats = (TabItem)root.findViewById(R.id.tabChats);
        tabStatus = (TabItem)root.findViewById(R.id.tabStatus);
        tabCalls = (TabItem)root.findViewById(R.id.tabCalls);
        viewPager = (ViewPager)root.findViewById(R.id.viewPager);

        pageAdapter = new PageAdapter(getChildFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(pageAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Handler handler = new Handler();

        handler.post(new Runnable() {
            @Override
            public void run() {
                //------------
            }
        });
    }
}

这是我的页面适配器代码: PageAdapter.java

public class PageAdapter extends FragmentPagerAdapter {

    private int numOfTabs;

    PageAdapter(FragmentManager fm, int tabCount) {
        super(fm);
        this.numOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new ChatFragment();
            case 1:
                return new StatusFragment();
            case 2:
                return new CallFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return numOfTabs;
    }

}

这是XML文件: fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="@android:color/black">

        <android.support.design.widget.TabItem
            android:id="@+id/tabChats"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chats" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabCalls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Calls" />

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

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

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我在我的项目中使用ButterKnife,所以如果您也不打算使用它,则可能要更改它。

    public class HomeFragment extends Fragment {

        @BindView(R.id.tabs)
        TabLayout mTabLayout;
        @BindView(R.id.pager)
        ViewPager mPager;
        View mView;

        PagerAdapter mAdapter;

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

        static MainFragment fragment;

        public static HomeFragment instance() {
            if (fragment == null) {
                fragment = new HomeFragment();
            }
            return fragment;
        }


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

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

            mPager.setAdapter(new PagerAdapter(getChildFragmentManager(), getActivity().getApplicationContext()));


            mTabLayout.setupWithViewPager(mPager);

            mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    mPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    mPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                    mPager.setCurrentItem(tab.getPosition());
                }
            });

            return mView;
        }

        private class PagerAdapter extends FragmentStatePagerAdapter {

            private String[] titles = {"Tab One", "Tab Two", "Tab Three"};

            public PagerAdapter(FragmentManager manager, Context context) {
                super(manager);
            }

            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return new TabOne();
                    case 1:
                        return new TabTwo();
                    case 2:
                        return new TabThree();
                    default:
                        return null;
                }

            }

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

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

        }


        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
        }

        @Override
        public void onDetach() {
            super.onDetach();
        }   
     }

这是fragment_home.xml

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="55dp"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            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"
                app:tabTextColor="#FFFFFF"
                app:tabSelectedTextColor="#FFFFFF"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

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

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>