TabLayout+Fragment+ViewPager小Demo

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;

    ArrayList<MyFragment> fragments;

    ViewPagerAdapter adapter;

    TabLayout tabLayout;

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

        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tablayout);

        //初始化数据
        fragments = new ArrayList<>();
        fragments.add(new MyFragment("111",null,R.layout.first_fragment));
        fragments.add(new MyFragment("222",null,R.layout.second_fragment));

        //设置ViewPager适配器
        adapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);

        //关联ViewPager
        tabLayout.setupWithViewPager(viewPager);
        //设置固定的tab
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="150dp"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/purple_200"
        app:tabIndicatorColor="#03A9F4"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorAnimationDuration="400"
        app:tabSelectedTextColor="#FF0000"
        app:tabTextColor="@color/white"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@color/cardview_dark_background"
        app:layout_constraintTop_toBottomOf="@id/tablayout"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MyFragement.java

public class MyFragment extends Fragment {
    private final String title;
    private final String content;
    private final int res;
    Context mContext;

    public MyFragment(String title, String content,int res){
        super();
        this.title = title;
        this.content = content;
        this.res = res;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

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

ViewPagerAdapter.java

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final ArrayList<MyFragment> fragments;

    public ViewPagerAdapter(FragmentManager fm, ArrayList<MyFragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

    /**
     * 得到页面标题
     * @param position The position of the title requested
     * @return
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return fragments.get(position).getTitle();
    }
}

first_fragment

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200">

</androidx.constraintlayout.widget.ConstraintLayout>

second_fragment

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

</androidx.constraintlayout.widget.ConstraintLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值