制表符不显示选项卡

问题描述 投票:1回答:4

我正在尝试使用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;

我有点迷路,我不知道为什么我的标签页不可见。预先感谢。

EDIT这是我的适配器:

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;
        }
    }

}
java android android-viewpager android-tablayout
4个回答
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/


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


0
投票

在管理标签之前,关键是首先到setupWithViewpager


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;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.