TabLayout+Fragment+ViewPager联合使用

    首先是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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值