实现TabLayout+ViewPager+Fragment头目的联动模式

//添加依赖

 compile 'com.android.support:design:24.1.1'

//这个依赖可以直接在系统找


//首先布局  应该能看懂吧



    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:orientation="vertical"
    tools:context="bwie.gouzeng.Main2Activity">


            android:id="@+id/tablelayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


//Activity


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


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


import static bwie.gouzeng.R.id.tablelayout;




public class Main2Activity extends AppCompatActivity {


    private String[] title = new String[]{"最新日报","专栏","热门","主题日报"};
    private TabLayout tabLayout;
    private ViewPager viewpager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tabLayout = (TabLayout) findViewById(tablelayout);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        for (String str : title){
            tabLayout.addTab(tabLayout.newTab().setText(str));
        }


        List fragments=new ArrayList<>();


        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragments.add(new Fragment4());


        MyAdapter adapter=new MyAdapter(getSupportFragmentManager(),title,fragments);


        //把集合放入适配器
        adapter.setFragments(fragments);
        //给ViewPager设置适配器
        viewpager.setAdapter(adapter);
        for (int i = 0; i             tabLayout.addTab(tabLayout.newTab());
        }
        //使tablayout和viewPager关联
        tabLayout.setupWithViewPager(viewpager);
        tabLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });
    }
}


//适配器

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


import java.util.List;




public class MyAdapter extends FragmentPagerAdapter {
    private String[] title;
    private List fragments;


    public MyAdapter(FragmentManager fm, String[] title, List fragments) {
        super(fm);
        this.title = title;
        this.fragments = fragments;
    }


    public void setFragments(List fragments) {
        this.fragments = fragments;
    }


    @Override
    public Fragment getItem(int position) {


        return fragments.get(position);
    }


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




    }


    @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }
}

你可能感兴趣的:(实现TabLayout+ViewPager+Fragment头目的联动模式)