TabLayout+ViewPager的使用
TabLayout+ViewPager可以用来实现选项卡切换
步骤:
1、在build.gradle中添加依赖
compile 'com.android.support:design:26.0.0-alpha1'
2、添加布局
xml:
[XML] 纯文本查看 复制代码 <?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
tabSelectedTextColor 选中时字体颜色
tabTextColor 未选中时颜色
tabIndicatorColor 下划线颜色
tabIndicatorHeight 下划线高度
-->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="45dp"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabIndicatorHeight="2dp"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@android:color/black" />
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3、java代码:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private ViewPager viewPager;
private List<Fragment> fragments;
private List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
tab = (TabLayout) findViewById(R.id.tablayout);
viewPager = (ViewPager) findViewById(R.id.vp);
fragments = new ArrayList<>();
list = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new ContactsFragment());
fragments.add(new DiscoverFragment());
fragments.add(new MyFragment());
list.add("首页");
list.add("联系人");
list.add("发现");
list.add("我");
}
private void initData() {
MyAdapter adapter = new MyAdapter(getSupportFragmentManager(), fragments, list);
viewPager.setAdapter(adapter);
tab.setupWithViewPager(viewPager);
adapter.notifyDataSetChanged();
}
}
4、适配器
MyAdapter:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2018/12/17.
*/
public class MyAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
private List<String> list;
public MyAdapter(FragmentManager fm, List<Fragment> fragments, List<String> list) {
super(fm);
this.fragments = fragments;
this.list = list;
}
//返回第几个fragment
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
//总共有多少个fragment
@Override
public int getCount() {
return fragments.size();
}
//返回title
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
}
看下效果:
|