Android--TabLayout的使用

首先需要依赖:implementation ‘com.android.support:design:28.0.0’

之后就是布局代码:

<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=".MyTab">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tl_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/cardview_dark_background"
        app:tabIndicatorColor="@android:color/white"
        app:tabTextColor="@android:color/white"
        app:tabSelectedTextColor="@android:color/holo_red_light"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

上个app分别的意思是:1,选中时,下面横线颜色。2,字体默认颜色。3,选中时,字体的颜色

Main代码:

public class MyTab extends AppCompatActivity {

    private List<MyFragment> list;
    private ViewPager viewPager;
    private MyFragmentAdapter adapter;
    private TabLayout tabLayout;

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

        tabLayout=(TabLayout)findViewById(R.id.tl_main);

        list=new ArrayList<>();
        for(int i=0;i<15;i++){
            list.add(new MyFragment("标题"+i));
        }

        viewPager=(ViewPager)findViewById(R.id.vp_main);
        //第二个参数不知道什么作用,随便打的。第一个就是...跟着视频打的,第三个就是自己传过去的
        adapter=new MyFragmentAdapter(getSupportFragmentManager(), 1,list);
        viewPager.setAdapter(adapter);

        //关联ViewPager
        tabLayout.setupWithViewPager(viewPager);
        //设置固定的标题列表
        //tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置不固定的标题列表
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}

设置Fragment视图:

public class MyFragment extends Fragment {

    private String title;

    public MyFragment(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

}

适配器:

public class MyFragmentAdapter extends FragmentPagerAdapter {

   private List<MyFragment> list;

    public MyFragmentAdapter(@NonNull FragmentManager fm, int behavior,List<MyFragment> list) {
        super(fm, behavior);
        this.list=list;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        MyFragment fragment=list.get(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }
    
    //得到标题
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getTitle();
    }
}

效果图:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值