Android中TabLayout与Fragment相结合

最近做了一个关于tabLayout的Demo,其中也涉及到了Fragment复用以及传值。

效果图


话不多说,直接上代码

首先需要导入TabLayout依赖

compile 'com.android.support:design:25.0.0'
网上还有另外一个版本的TabLayout的依赖,根据自己的需要选择就好
compile 'com.android.support:design:22.2.0'

主页面布局
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/mTb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
//选中时游标的颜色
 app:tabIndicatorColor="@color/red"
//选中时的字体颜色
 app:tabSelectedTextColor="@color/red"
//未选中时的字体颜色
 app:tabTextColor="@color/gray">
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/mVp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
公用fragment布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_first_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabLayout是一种常见的Android UI控件,可以用于在Activity或Fragment创建多个选项卡,每个选项卡可以加载不同的Fragment。下面是实现TabLayout和Fragment的步骤: 1. 在布局文件添加TabLayout和ViewPager控件,如下所示: ``` <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill"/> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 2. 创建Fragment类,例如: ``` public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my, container, false); //TODO: add your fragment UI code here return view; } } ``` 3. 创建FragmentPagerAdapter类,用于管理ViewPager和Fragment之间的关系,例如: ``` public class MyPagerAdapter extends FragmentPagerAdapter { private final String[] tabTitles = new String[]{"Tab1", "Tab2"}; public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new MyFragment(); case 1: return new MyFragment(); default: return null; } } @Override public int getCount() { return tabTitles.length; } @Override public CharSequence getPageTitle(int position) { return tabTitles[position]; } } ``` 4. 在Activity或Fragment初始化TabLayout和ViewPager,并设置PagerAdapter,例如: ``` TabLayout tabLayout = findViewById(R.id.tab_layout); ViewPager viewPager = findViewById(R.id.view_pager); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager); ``` 现在你就可以在TabLayout看到两个选项卡,每个选项卡都加载了相同的Fragment。你可以根据需要修改Fragment的布局和逻辑,以及PagerAdapter的getItem和getPageTitle方法,来实现自己的界面和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值