欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TabLayout使用

程序员文章站 2023-12-24 19:26:03
...

AFragment

package com.bwie.zhaojunhui20181113;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tabLayout = view.findViewById(R.id.app_tab);
        viewPager = view.findViewById(R.id.viewPager);

        viewPager.setAdapter(new TabPagerAdapter(getChildFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

    }

}

fragment01.xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffffcc"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <android.support.design.widget.TabLayout
       android:id="@+id/app_tab"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:tabMode="scrollable"
       />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_tab"
        app:layout_constraintBottom_toBottomOf="parent"
        />


</android.support.constraint.ConstraintLayout>

TabAdapter适配器

package com.bwie.zhaojunhui20181113;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;

public class TabPagerAdapter extends FragmentPagerAdapter {

    private String[] pageName = new String[]{"关注","推荐","热点"};

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

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new Fragment_a();
            case 1:
                return new Fragment_b();
            case 2:
                return new Fragment_c();
             default:
                 return new Fragment_a();
        }
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return pageName[position];
    }

    @Override
    public int getCount() {
        return pageName.length;
    }
}

相关标签: TabLayout

上一篇:

下一篇: