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();
    }
}

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

原创文章 158 获赞 2 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/104714817