TabLayout+Fragment+ViewPager联合使用

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





    
    



    



然后是fragment的界面page_one.xml




    


然后是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 list;//ViewPager要填充的fragment列表
    Listtitle;//tab中的title文字列表
    //使用构造方法来将数据传进去
    public MyPagerAdapter(FragmentManager fm, List list,Listtitle) {
        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 mTitleList = new ArrayList<>();
    private ListmViewList = 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);

    }

}


你可能感兴趣的:(android)