【问题标题】:Tabs not showing Tablayout + ViewPager标签不显示 Tablayout + ViewPager
【发布时间】:2016-11-14 08:37:21
【问题描述】:

标签内容已呈现,我可以在它们之间滑动,但标签不显示。

activity_home.xml(请注意我如何将 TabLayout 嵌套在 ViewPager 下,如 here 所述)

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_activity_DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:elevation="2dp" />
        </android.support.v4.view.ViewPager>

    </LinearLayout>
    <!-- The navigation drawer -->
    <include layout="@layout/navigation_drawer" />

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

HomeActivity.java

public class HomeActivity extends AppCompatActivity {

    // Declaring Your View and Variables

    Toolbar mToolbar;
    ViewPager mViewPager;
    ViewPagerAdapter mViewPagerAdapter;
    TabLayout mSlidingTabs;
    CharSequence mTabTitles[] = {"Home", "Events"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        initSlidingTabs();
    }

    private void initSlidingTabs() {
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mTabTitles);

        // Assigning ViewPager View and setting the mViewPagerAdapter
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mViewPagerAdapter);

// // 也尝试了以下但没有任何变化 // mSlidingTabs = (TabLayout) findViewById(R.id.tabs); // mSlidingTabs.addTab(mSlidingTabs.newTab().setText("Tab 1")); // mSlidingTabs.addTab(mSlidingTabs.newTab().setText("Tab 2")); // mSlidingTabs.setupWithViewPager(mViewPager); } }

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence mTabTitles[]; // This will Store the mTabTitles of the Tabs which are Going to be passed when ViewPagerAdapter is created

    // Build a Constructor and assign the passed Values to appropriate values in the class
    public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[]) {
        super(fm);

        this.mTabTitles = mTitles;
    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {
        if(position == 0) // if the position is 0 we are returning the First tab
        {
            Tab1 tab1 = new Tab1();
            return tab1;
        }
        else             // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            Tab2 tab2 = new Tab2();
            return tab2;
        }
    }

    // This method return the titles for the Tabs in the Tab Strip
    @Override
    public CharSequence getPageTitle(int position) {
        return mTabTitles[position];
    }

    // This method return the Number of tabs for the tabs Strip
    @Override
    public int getCount() {
        return mTabTitles.length;
    }
}

Tab1.java

public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1,container,false);
        return v;
    }
}

【问题讨论】:

  • 我也有同样的问题。在 ViewPager 中使用 TabLayout 就像它记录在案的那样,但我没有看到标签。你找到解决办法了吗?

标签: android android-viewpager android-tablayout


【解决方案1】:

为什么要将 TabLayout 放在 ViewPager 中? 您还需要调用 tabLayout.setupWithViewPager(viewPager);

【讨论】:

  • 嗨@AmmarMohammed,1)我将TabLayout 放在ViewPager 中,因为文档是这样说的(我的问题中的链接)2)是的,我试过了,没有任何改变在我的问题中
  • 尝试将 TabLayout 放在 AppBarLayout 内的 Toolbar 下,我使用了完全相同的结构,它对我有用。
  • 但是为什么呢? @AmmarMohammed,这对我来说听起来像是一个黑客。您是说 Google 的文档在这里有误吗?
  • 试试吧,老实说我还没有阅读文档,但据我所知,我正在学习一些教程。
【解决方案2】:

你可以试试这个。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_activity_DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v4.view.ViewPager>
        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:elevation="2dp" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <include layout="@layout/navigation_drawer" />

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

【讨论】:

  • 谢谢,但您将TabLayout 放在ViewPager 之外,这违背了我的问题的目的
【解决方案3】:

如果一个 View 实现了 Decor,那么 ViewPager 会将其视为一个特殊的 View,可以保留在 ViewPager 之上。这通常用于实现您自己的选项卡。

有关更多信息,请访问以下链接: https://developer.android.com/reference/android/support/design/widget/TabLayout.html

此视图还支持用作 ViewPager 装饰的一部分,并且 可以直接添加到布局资源中的ViewPager

装饰: https://developer.android.com/reference/android/support/v4/view/ViewPager.html

使用 ViewPager.DecorView 注释的视图是 视为视图寻呼机“装饰”的一部分。每个装饰视图的位置 可以通过它的 android:layout_gravity 属性来控制

我想为了更好的理解,你可以通过这个blog

最终结论:

你必须执行

来自接口 java.lang.annotation.Annotation

【讨论】:

    【解决方案4】:

    我现在也有同样的问题。

    按照 Google 的文档,通过在 ViewPager 中嵌套 TabLayout工作。

    按照 Google 的文档,将 PagerTabStrip 嵌套在 ViewPager确实有效。


    解决方案

    TabLayout 嵌套在ViewPager 中。将其紧邻ViewPager

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多