标签不会出现在TabLayout中

时间:2015-12-18 02:26:38

标签: java android android-viewpager android-tablayout

我试图通过3个标签获得一个简单的TabLayout。按照不同的教程,我的标签不会显示在最终结果上(只有3个没有文字的空标签)。

这是我尝试添加这些标签的方法:

首先是xml布局容器ViewPager(communitylayout)

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:padding="4dip"
        android:layout_above="@+id/bottomcontent3"
        android:gravity="center_horizontal"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/compager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>

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

Tab xml(tabtext):

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

    <TextView
        android:id="@+id/tabtext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/tab_text_color"
        />

</RelativeLayout> 

最后我尝试添加这些标签的java类:

super.onSaveInstanceState(savedInstanceState);
        view = inflater.inflate(R.layout.communitylayout, container, false); // communitylayout is the first xml I put, see above
        Bundle data = getArguments();
        MainActivity.addShapeAndBottom(inflater, view, "com", this);

        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.comtabs);

        // add tabs
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());

        RelativeLayout layout1 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);
        RelativeLayout layout2 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);
        RelativeLayout layout3 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);

            //tab is an array of Strings containing the Tab name  


((TextView)layout1.findViewById(R.id.tabtext)).setText(tabs[0]);
((TextView)layout2.findViewById(R.id.tabtext)).setText(tabs[1]);
((TextView)layout3.findViewById(R.id.tabtext)).setText(tabs[2]);


        tabLayout.getTabAt(0).setCustomView(layout1);
        tabLayout.getTabAt(1).setCustomView(layout2);
        tabLayout.getTabAt(2).setCustomView(layout3);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);



        ViewPager pager = (ViewPager) view.findViewById(R.id.compager);
        CommunityPagerFragment adapter = new CommunityPagerFragment(getChildFragmentManager());

        pager.setAdapter(adapter);
        tabLayout.setupWithViewPager(pager);
        //tabLayout.set

        return view;

我有点迷失,我不知道为什么我的标签仍然不可见。提前谢谢。

编辑这是我的适配器:

public class CommunityPagerFragment extends FragmentPagerAdapter {

    public CommunityPagerFragment(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                return new BlogFragment();
            case 1:
                return new NewsFragment();
            case 2:
                return new FAQFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }




    public class BlogFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.communitylistview, container, false);
            return view;
        }
    }

    public class NewsFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.communitylistview, container, false);
            /*ProgressBar loadingAnim = new ProgressBar(getContext());
            loadingAnim.setLayoutParams(new LinearLayout.LayoutParams(40,40));
            container.addView(loadingAnim);*/

            return view;
        }
    }

    public class FAQFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.communitylistview, container, false);
            return view;
        }
    }

}

4 个答案:

答案 0 :(得分:1)

你有一个viewpager,为什么不使用setupWithViewPager方法?

getTabbar().setupWithViewPager(viewPager);

将片段添加到适配器

BaseFragmentPagerAdapter adapter = new BaseFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ExampleListedRecyclerViewFragment(), "Tab title 1", false);
adapter.addFragment(new ExampleGridedRecyclerViewFragment(), "Tab title 2", false);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);

ViewPager适配器

public  class BaseFragmentPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

    public BaseFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        addFragment(fragment, title, true);
    }

    public void addFragment(Fragment fragment, String title, boolean hasOptionsMenu) {
        fragment.setHasOptionsMenu(hasOptionsMenu);
        mFragments.add(fragment);
        mFragmentTitles.add(title);

    }

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

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

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

如果要将图标设置为标签项而不是文本标题:

tabLayout.getTabAt(i).setIcon(iconDrawable);

更多详情请参阅我的项目

https://github.com/DanielShum/MaterialAppBase/blob/master/materialAppBaseLibrary/src/main/java/com/daililol/material/appbase/base/BaseTabbableActionbarActivity.java

https://github.com/DanielShum/MaterialAppBase/

答案 1 :(得分:0)

试试这个: 元素选项卡XML您想要的位置:

 <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            ></android.support.design.widget.TabLayout>

在您的MainActivity中:

TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
        tabs.setTabMode(TabLayout.MODE_FIXED);
        tabs.addTab(tabs.newTab().setText("Tab 1"));
        tabs.addTab(tabs.newTab().setText("Tab 2"));
        tabs.addTab(tabs.newTab().setText("Tab 3"));

enter image description here

答案 2 :(得分:0)

在管理标签页之前,关键是setupWithViewpager

答案 3 :(得分:0)

setupWithViewpager首先,然后管理标签。

xml布局:

<android.support.v7.widget.Toolbar`enter code here`
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/coloricon" />

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

    <android.support.design.widget.TabItem
        android:id="@+id/tab1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <android.support.design.widget.TabItem
        android:id="@+id/tab2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="@android:color/white"/>

在片段中:

public class myfragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View V = inflater.inflate(R.layout.fragment_xml, container, false);

        TabLayout tabLayout = V.findViewById(R.id.tablayout);
        TabItem tabScan = V.findViewById(R.id.tab1);
        TabItem tabCreate = V.findViewById(R.id.tab2);
        final ViewPager viewPager = (ViewPager) V.findViewById(R.id.viewpager);
        viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount()));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//setupWithViewpager first before managing the tabs.

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.getTabAt(0).setText("1");
        tabLayout.getTabAt(1).setText("2");
        tabLayout.setOnTabSelectedListener(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 V;

    }
    public class PagerAdapter extends FragmentStatePagerAdapter {
        int mNumOfTabs;

        public PagerAdapter(FragmentManager fm, int NumOfTabs) {
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }


        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new fragment1();
                case 1:
                    return new fragment2();
                default:
                    return null;
            }
        }

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

}