效果图

Tablayout的使用_tablayout

java代码 butterknife使用 见我其他的博客 

MainActivity

package com.example.liuan.tablayout.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
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.widget.LinearLayout;

import com.example.liuan.tablayout.adapter.Find_tab_Adapter;
import com.example.liuan.tablayout.Fragment.Find_hotCollectionFragment;
import com.example.liuan.tablayout.Fragment.Find_hotMonthFragment;
import com.example.liuan.tablayout.Fragment.Find_hotRecommendFragment;
import com.example.liuan.tablayout.Fragment.Find_hotToday;
import com.example.liuan.tablayout.R;

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

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


@Bind(R.id.tl_title)
TabLayout tlTitle;
@Bind(R.id.vp)
ViewPager vp;
@Bind(R.id.activity_main)
LinearLayout activityMain;
private Find_hotRecommendFragment hotRecommendFragment;
private Find_hotCollectionFragment hotCollectionFragment;
private Find_hotMonthFragment hotMonthFragment;
private Find_hotToday hotToday;
private List<Fragment> list_fragment; //定义要装fragment的列表
private List<String> list_title;
private Find_tab_Adapter find_tab_adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);

initData();
find_tab_adapter = new Find_tab_Adapter(getSupportFragmentManager(),list_title);

//viewpager加载adapter
vp.setAdapter(find_tab_adapter);
//关联指示器
tlTitle.setupWithViewPager(vp);
}

private void initData() {

//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
list_title = new ArrayList<>();
list_title.add("热门推荐");
list_title.add("热门收藏");
list_title.add("本月热榜");
list_title.add("今日热榜");
//设置tabLayout的模式
// tlTitle.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加名称
for (int i = 0; i < 4; i++) {
tlTitle.addTab(tlTitle.newTab().setText(list_title.get(i)));
}



}
}




Find_tab_Adapter

package com.example.liuan.tablayout.adapter;

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

import com.example.liuan.tablayout.Factory.FragmentFactory;

import java.util.List;

/**
* Name: Find_tab_Adapter
* Author: liuan
* creatTime:2017-01-08 07:13
*/

public class Find_tab_Adapter extends FragmentPagerAdapter {

//tab名的列表
private List<String> list_title;

public Find_tab_Adapter(FragmentManager fm, List<String> list_title) {

super(fm);
=
this.list_title=list_title;

}


@Override
public Fragment getItem(int position) {
return FragmentFactory.createFragment(position);
}


@Override
public int getCount() {
return list_title.size();
}
//此方法 用来显示tab上的名字

@Override
public CharSequence getPageTitle(int position) {
return list_title.get(position%list_title.size());
}
}



BaseFragment

package com.example.liuan.tablayout.base;

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

/**
* Name: BaseFragment
* Author: liuan
* creatTime:2017-01-08 13:48
*/

public class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setText(getClass().getSimpleName());
return textView;
}
}



FragmentFactory

package com.example.liuan.tablayout.Factory;

import com.example.liuan.tablayout.Fragment.Find_hotCollectionFragment;
import com.example.liuan.tablayout.Fragment.Find_hotMonthFragment;
import com.example.liuan.tablayout.Fragment.Find_hotRecommendFragment;
import com.example.liuan.tablayout.Fragment.Find_hotToday;
import com.example.liuan.tablayout.base.BaseFragment;

import java.util.HashMap;

/**
* Name: FragmentFactory
* Author: liuan
* creatTime:2017-01-08 15:23
*/

public class FragmentFactory {
private static HashMap<Integer,BaseFragment> hm=new HashMap<>();
public static BaseFragment createFragment(int position){
BaseFragment baseFragment = hm.get(position);
if(baseFragment==null){
switch (position){
case 0:
baseFragment=new Find_hotRecommendFragment();
break;
case 1:
baseFragment=new Find_hotCollectionFragment();
break;
case 2:
baseFragment=new Find_hotMonthFragment();
break;
case 3:
baseFragment = new Find_hotToday();
break;
}
hm.put(position,baseFragment);
}
return baseFragment;
}
}


//下面这四个是完全一样的 除了名字 所以我就来一个了 

Find_hotCollectionFragment

Find_hotMonthFragment

Find_hotRecommendFragment

Find_hotToday

package com.example.liuan.tablayout.Fragment;

import com.example.liuan.tablayout.base.BaseFragment;

/**
* Name: Find_hotToday
* Author: liuan
* creatTime:2017-01-08 07:44
*/
public class Find_hotToday extends BaseFragment {
}



布局代码

activity_main.xml


<LinearLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.TabLayout
android:id="@+id/tl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000ff"
app:tabIndicatorColor="#ffffff"
app:tabSelectedTextColor="#aaa"
app:tabTextColor="#ff0000" />

<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>





//依赖

compile 'com.android.support:design:24.2.0'
compile 'com.jakewharton:butterknife:7.0.1'