片段中的Tablayout未使用视图寻呼机显示制表符

时间:2016-06-17 07:06:49

标签: android android-fragments android-viewpager android-tablayout

我正在尝试使用视图寻呼机和查看寻呼机适配器在片段中添加2个标签,但它们无法显示。

[![this is how it looks, no tabs are showing][1]][1]

这是 fragment_home.xml

<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"
    tools:context="com.code.erum.masterappeverycatalogdemo.activity.LatestBrandsFragment">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>
</RelativeLayout>

这是我的 ViewPagerAdapter 类,它在片段列表中添加片段

class ViewPagerAdapter extends FragmentPagerAdapter {
            private final List<Fragment> mFragmentList = new ArrayList<>();
            private final List<String> mFragmentTitleList = new ArrayList<>();

            public ViewPagerAdapter(FragmentManager manager) {
                super(manager);
            }

            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

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

            public void addFragment(Fragment fragment, String title) {
                mFragmentList.add(fragment);
                mFragmentTitleList.add(title);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentTitleList.get(position);
            }
        }

这是我的主页片段

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

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

            tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
            tabLayout.setupWithViewPager(viewPager);
            // Inflate the layout for this fragment
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }

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

        private void setupViewPager(ViewPager viewPager) {
            ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
            adapter.addFragment(new LastestCatalogsFragment(), "ONE");
            adapter.addFragment(new LatestBrandsFragment(), "TWO");
            viewPager.setAdapter(adapter);
        }

这是 gradle

apply plugin: 'com.android.application'

          android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"

        defaultConfig {
            applicationId "com.code.erum.masterappeverycatalogdemo"
            minSdkVersion 17
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }

        dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:22.0.0'
        compile 'com.android.support:recyclerview-v7:22.0.0'
        compile 'com.android.support:design:22+'
        }

2 个答案:

答案 0 :(得分:1)

更改此内容:

private void setupViewPager(ViewPager viewPager) {

    FragmentManager FManager = getChildFragmentManager();
    ViewPagerAdapter adapter = new ViewPagerAdapter(FManager);

    // ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
    // ...
}

答案 1 :(得分:0)

<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.code.erum.masterappeverycatalogdemo.activity.LatestBrandsFragment">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"/>
</LinearLayout>
相关问题