gradle中添加依赖
*compile'com.android.support:design:26+'
/** * 1--写tablayout * 2--- 写viewPager * 3-- 写viewPagerAdapter * 4-- 让TabLayout和Viewpager关联; */
布局文件代码
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.tablayoutdemo2.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
</LinearLayout>
--------------------------
MainActivity代码, 注意FragmentPagerAdapt中要重写getPageTitle方法;
package com.bwie.tablayoutdemo;
import android.os.Bundle;
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.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
//放所有的fragment
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
// tablayout的标题
ArrayList<String> titleList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
initData();
// viwepager的适配器
MPagerAdapter mPagerAdapter = new MPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mPagerAdapter);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);// tab的显示模式
// 让标题和viewpager关联
tabLayout.setupWithViewPager(viewPager);
}
private void initData() {
for (int i = 0; i < 6; i++) {
titleList.add("标题"+i);
fragmentList.add(new BlankFragment());
}
}
private class MPagerAdapter extends FragmentPagerAdapter{
public MPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return titleList.size();
}
/**
* 重写这个方法,返回标题****
* @param position
* @return
*/
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
}
---------------------
package com.bwie.tablayoutdemo;
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;
public class BlankFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
Bundle bundle = getArguments();
// String name = bundle.getString("name");
double random = Math.random();
textView.setText("xxxxxxxxxxx"+random);
return textView;
}
//静态创建自己实例的方法, 并且传递值过来,返回自己;
public static BlankFragment getInstances(String name) {
BlankFragment blankFragment = new BlankFragment();
Bundle bundle = new Bundle();
bundle.putString("name", name);
blankFragment.setArguments(bundle);
return blankFragment;
}