简体   繁体   English

如何使用 tabLayout 在 ViewPager 中显示片段?

[英]How to show fragments in ViewPager with tabLayout?

I'm trying to show fragments in viewPager with tabsLayout but the viewPager shows nothing.我正在尝试使用 tabsLayout 在 viewPager 中显示片段,但 viewPager 不显示任何内容。 However, the tabs have been added to the view.但是,选项卡已添加到视图中。 I have also put some logs in the fragments just to make sure that they're created and it's showing the logs - nothing wrong with the fragments.我还在片段中放入了一些日志,以确保它们已创建并显示日志 - 片段没有问题。

the layout that contains the tabs and viewPager:包含选项卡和 viewPager 的布局:

 <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextAppearance="@style/customTabsStyle"
            app:tabTextColor="#000" />
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>

Then in MainActivity I have this code:然后在 MainActivity 中我有这段代码:

    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.promotions)));
    tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.all_stores)));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    final HomeTabsAdapter tabsAdapter = new 
    HomeTabsAdapter(MainActivity.this,getSupportFragmentManager(),
            tabLayout.getTabCount());
    viewPager.setAdapter(tabsAdapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(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) {
    }
});

And this is part of HomeTabsAdapter:这是 HomeTabsAdapter 的一部分:

public HomeTabsAdapter(Context c, FragmentManager fm, int totalTabs) {
super(fm);
context = c;
this.totalTabs = totalTabs;

} }

@NonNull
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 1:
            return getAllStoresFragment();
        default:
            return getPromotionsFragment();
    }

Note: Both fragments are of the same type but i'm showing different stuff based on the Arguments. (I'm using the getInstance(..) way to handle this using Bundle and Arguments).注意:这两个片段属于同一类型,但我基于 Arguments 展示了不同的东西。(我使用 getInstance(..) 方法通过 Bundle 和 Arguments 来处理这个问题)。

What changes to this code could solve the issue?对此代码进行哪些更改可以解决问题?

The position starts at 0. position 从 0 开始。

@NonNull
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return getAllStoresFragment();
        default:
            return getPromotionsFragment();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM