美文网首页
Android Tablayout+ViewPager+Frag

Android Tablayout+ViewPager+Frag

作者: JYangkai | 来源:发表于2019-02-24 21:44 被阅读0次

正文

我们在进行开发的时候可能需要使用Fragment,更多时候是结合Tablayout和ViewPager来使用,我们的使用步骤一般是

  1. 获取Tablayout和ViewPager控件的实例
  2. 初始化各个Fragment
  3. 初始化自定义的FragmentPagerAdapter
  4. 给ViewPager设置适配器,即设置FragmentPagerAdapter
  5. 让Tablayout和ViewPager绑定

下面就让我们来看看具体怎么做

XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top">

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.TabLayout
        android:id="@+id/main_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:layout_gravity="bottom"
        app:tabIndicatorHeight="0dp"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="@color/colorWhite" />

</android.support.design.widget.CoordinatorLayout>

获取实例

mTabLayout = findViewById(R.id.main_tab_layout);
mViewPager = findViewById(R.id.main_view_pager);

初始化Fragment

这个Fragment可以是你自定义的Fragment,也可以是原生的Fragment,不过一般都是用自定义的Fragment,所以在这里就不写具体的代码

自定义适配器FragmentPagerAdapter

package com.yk.mchat.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;

    private List<Fragment> fragmentList;

    private List<String> titles;

    public MyFragmentPagerAdapter(FragmentManager fm, Context mContext, List<Fragment> fragmentList, List<String> titles){
        super(fm);
        this.mContext=mContext;
        this.fragmentList=fragmentList;
        this.titles=titles;
    }

    @Override
    public Fragment getItem(int i) {
        return fragmentList.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

这个是我们自定义的适配器
接着我们就可以初始化适配器了

mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this, mFragmentList, mTabTitles);

设置适配器

mViewPager.setAdapter(mAdapter);

Tablayout绑定ViewPager

mTabLayout.setupWithViewPager(mViewPager);

这样,我们的分页就做好了,这个就是Tablayout+ViewPager+Fragment,希望大家能够喜欢,如果有写错的或者不太规范的,望大家能评论指正。

相关文章

网友评论

      本文标题:Android Tablayout+ViewPager+Frag

      本文链接:https://www.haomeiwen.com/subject/defiyqtx.html