前两篇文章中介绍了TabLayout和ViewPgaer+Fragment的使用,这篇文章将三者结合起来,做成一个比较常见的布局,如图:
如果对着这两者有什么不清楚的,可以看看这两篇,都是一些简单的使用。
废话不多说,直接上代码。
要使用TabLayout,必须先在app的build.gradle中添加:
compile 'com.android.support:design:23.1.1'
在主布局中添加TabLayout和ViewPager
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorPrimaryDark"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/colorAccent" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp">
</android.support.v4.view.ViewPager>
</LinearLayout>
创建两个Fragement类
fragment1.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"
android:background="@color/colorAccent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment1"
/>
</LinearLayout>
fragment2.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"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment2"
/>
</LinearLayout>
Fragment1.class
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.fragment1, container, false);
return view;
}
}
Fragment2.class
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view= inflater.inflate(R.layout.fragment2, container, false);
return view;
}
}
创建ViewPager的适配器
FragAdapter.calss
public class FragAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> title;
public FragAdapter(FragmentManager fm,List<Fragment> fragments,List<String> titles) {
super(fm);
fragmentList = fragments;
title = titles;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
}
其中getPageTitle方法是获取标题的。
MainActivity中
public class MainActivity extends AppCompatActivity {
private TabLayout mTabLayout;
//标题列表
ArrayList<String> titleList= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//构造适配器
List<Fragment> fragments=new ArrayList<Fragment>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
titleList.add("武侠");
titleList.add("科幻");
FragAdapter adapter = new FragAdapter(getSupportFragmentManager(), fragments,titleList);
//设定适配器
ViewPager vp = (ViewPager)findViewById(R.id.vp);
vp.setAdapter(adapter);
mTabLayout= (TabLayout) findViewById(R.id.tab);
mTabLayout.setupWithViewPager(vp);
}
}
到此,最简单的使用方法就结束了。当然,TabLayout的样式属性有很多,大家可以试着修改一下,做出自己想要的标题。还有就是TabLayout的属性中没有修改下划线宽度的方法,需要我们自己去写代码修改。这个我在后面的文章中会补充的。
最后附上遇到的一个小问题:
ClassNotFoundException: Didn't find class "android.support.v7.internal.widget.TintManager
就是这句代码:indiactor.setupWithViewPager(vpnewInfo);
在给TabLayout关联ViewPager的时候报错,
在引入design包的时候,版本要和appcompat版本一致。
compile 'com.android.support:design:25.0.0' compile 'com.android.support:appcompat-v7:25.0.0'