TabLayout+ViewPager+Fragment实现页面切换显示

本文介绍了如何使用TabLayout、ViewPager和Fragment实现页面切换显示的步骤,包括在界面添加控件、初始化数据、添加Tab标签、设置适配器以及关联TabLayout与ViewPager。提供了相应的代码示例和资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现步骤如下:

一:界面上写TabLayout与ViewPager两个控件

二:初始化数据(标题+Fragment)

三:添加Tab标签到TabLayout

四:设置适配器(ViewPager的适配器)

五:将TabLayout与ViewPager关联起来

一:界面上写TabLayout与ViewPager两个控件

代码如下:

<com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tbl_tablayout"
        app:tabIndicatorColor="#F00"
        app:tabSelectedTextColor="#F00"
        app:tabTextColor="#1d1c1d"
        app:tabMode="fixed">
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="115dp" />

其中:TabLayout中的可以设置一些变量

app:tabIndicatorColor="#F00"<!-- 下方滚动的下划线颜色  -->
app:tabSelectedTextColor="#F00" <!-- tab被选中后,文字的颜色  -->
app:tabTextColor="#1d1c1d"<!-- tab中字体的颜色  -->
app:tabIndicatorHeight="5dp"<!--指示条的高度-->
app:tabTextAppearance="@style/App_Theme"<!--可以改变tab标签的字体的大小-->

三:添加Tab标签到TabLayout

tblTablayout.addTab(tblTablayout.newTab().setText(titles.get(i)));

四:设置适配器(ViewPager的适配器)

在适配器中必须重写getPageTitle()方法要不然标题是不会显示出来的

public CharSequence getPageTitle(int position) {
    return titles.get(position);
}

五:将TabLayout与ViewPager关联起来

tblTablayout.setupWithViewPager(vpViewpager);

实例代码如下:

Activity:

public class TabLayoutActivity extends BaseActivity {

    @BindView(R.id.tbl_tablayout)
    TabLayout tblTablayout;
    @BindView(R.id.vp_viewpager)
    ViewPager vpViewpager;

    private List<String> titles = new ArrayList<>();//放标题
    private List<Fragment> fragments = new ArrayList<>();//放fragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
//添加fragment
        fragments.add(TabLayoutItemFragment.newInstance("111111", "111111"));
        fragments.add(TabLayoutItemFragment.newInstance("222222", "222222"));
        fragments.add(TabLayoutItemFragment.newInstance("333333", "333333"));
        fragments.add(TabLayoutItemFragment.newInstance("444444", "444444"));
//添加标题
        titles.add("fragment1");
        titles.add("fragment2");
        titles.add("fragment3");
        titles.add("fragment4");
//添加tab标签
        for (int i = 0; i < titles.size(); i++) {
            tblTablayout.addTab(tblTablayout.newTab().setText(titles.get(i)));
        }
//添加设置适配器
        vpViewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
//把TabLayout与ViewPager关联起来
        tblTablayout.setupWithViewPager(vpViewpager);
    }
    class ViewPagerAdapter extends FragmentStatePagerAdapter{

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

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

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

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
//让标题显示出来
            return titles.get(position);
        }
    }


}

xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".activity.TabLayoutActivity">


    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tbl_tablayout"
        app:tabIndicatorColor="#F00"
        app:tabSelectedTextColor="#F00"
        app:tabTextColor="#1d1c1d"
        app:tabMode="fixed">
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="115dp" />

</LinearLayout>

希望能对大家实现这样的功能有所帮助

代码地址:https://github.com/xhh1234/StudyViews.git

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值