关闭

TabLayout+Fragment+ViewPager联合使用

标签: android
2636人阅读 评论(0) 收藏 举报
分类:

    首先是activity_main.xml文件,定义主要的界面,因为我用了FloatingActionBar,所以最外层用的是CoordinatorLayout,在这里面嵌入一个LinearLayout,在LinearLayout中放入TabLayout和ViewPager。在TabLayout中app:tabTextColor 设定未点击时,tab中文字的颜色,app:tabSelectedTextColor是点击时tab中文字的颜色。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.shaoshuai.houseestate.MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabTextColor="@color/colorPrimary"
        app:tabSelectedTextColor="@color/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email"/>

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


然后是fragment的界面page_one.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv"
        android:text="3"
        android:textSize="40sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>


然后是MyPagerAdapter.class,在其中继承FragmentPagerAdapter,并将内容填充进去。


package com.shaoshuai.houseestate;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by shaoshuai on 16/3/9.
 */
class MyPagerAdapter extends FragmentPagerAdapter {
    List<NumberFragment> list;//ViewPager要填充的fragment列表
    List<String>title;//tab中的title文字列表
    //使用构造方法来将数据传进去
    public MyPagerAdapter(FragmentManager fm, List<NumberFragment> list,List<String>title) {
        super(fm);
        this.list = list;
        this.title = title;
    }

    @Override
    public NumberFragment getItem(int position) {//获得position中的fragment来填充
        return list.get(position);
    }

    @Override
    public int getCount() {//返回FragmentPager的个数
        return list.size();
    }

    //FragmentPager的标题,如果重写这个方法就显示不出tab的标题内容
    @Override
    public CharSequence getPageTitle(int position) {
        return title.get(position);
    }
}

然后是定义的Fragment。用Bundle把newInstanc方法中获得的数据传入Fragment。


package com.shaoshuai.houseestate;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by shaoshuai on 16/3/9.
 */
public class NumberFragment extends android.support.v4.app.Fragment {
    private int mNumber;
    public static NumberFragment newInstance(int number) {

        Bundle args = new Bundle();
        args.putInt("123",number);
        NumberFragment fragment = new NumberFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNumber = getArguments().getInt("123");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.pager_one, container, false);
        TextView tv = (TextView) v.findViewById(R.id.tv);
        tv.setText("fragment:"+mNumber);
        return v;
    }

}

最后是MainActivity.class,在onCreate方法中把TabLayout和ViewPager初始化,最后调用setAdapter()方法来设定ViewPager的Adapter,使用setupWithViewPager()方法来让TabLayout和ViewPager绑定,这样就能点击tab时,ViewPager一起动,滑动ViewPager时Tab能一起动。


package com.shaoshuai.houseestate;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private List<String> mTitleList = new ArrayList<>();
    private List<NumberFragment>mViewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FloatingActionButton actionButton = (FloatingActionButton) findViewById(R.id.fab);
        actionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"sssss",Toast.LENGTH_LONG).show();
            }
        });
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);

        NumberFragment fragment1 = NumberFragment.newInstance(1);
        NumberFragment fragment2 = NumberFragment.newInstance(2);
        NumberFragment fragment3 = NumberFragment.newInstance(3);

        mViewList.add(fragment1);
        mViewList.add(fragment2);
        mViewList.add(fragment3);

        mTitleList.add("Tab1");
        mTitleList.add("Tab2");
        mTitleList.add("Tab3");

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.addTab(mTabLayout.newTab());
        mTabLayout.addTab(mTabLayout.newTab());
        mTabLayout.addTab(mTabLayout.newTab());

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager()
                                                          ,mViewList
                                                          ,mTitleList);
        mViewPager.setAdapter(myPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabsFromPagerAdapter(myPagerAdapter);

    }

}


2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8119次
    • 积分:325
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条