Introduction
When we use viewpager, we often use TabPageIndicator to cooperate with it. Achieve a very beautiful effect. But TabPageIndicator is a third-party, and it is relatively old. Of course, many great gods have begun to write TabPageIndicator by themselves to meet their own needs. At the Google Conference in 2015, Google released a new Android Support Design library, which contains Among several new controls, there is a TabLayout, which can complete the effect of TabPageIndicator, and it is also official. The best is that it can be compatible with versions above 2.2, including 2.2.
use
- Reference library: compile'com.android.support :design: 24.2.0'
Case study
- Main Acitity
package com.example.dell.myapplication;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
TabLayout tabLayout;
ArrayList<MyFragment> fragments;
ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//初始化数据
fragments = new ArrayList<>();
for (int i=0;i<12;i++){
fragments.add(new MyFragment("标题"+i,"内容"+i));
}
//设置ViewPager的适配器
adapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments);
viewPager.setAdapter(adapter);
//关联ViewPager
tabLayout.setupWithViewPager(viewPager);
//设置固定的
// tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}
}
- MyFragment
package com.example.dell.myapplication;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by dell on 2020/2/4.
*/
public class MyFragment extends Fragment {
private final String title;
private final String content;
Context mContext;
TextView textView;
public String getContent() {
return content;
}
public String getTitle() {
return title;
}
public MyFragment(String title, String content){
super();
this.title = title;
this.content = content;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//上下文
mContext = getActivity();
}
/**
* 创建视图
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//创建视图
textView = new TextView(mContext);
textView.setTextColor(Color.RED);
textView.setTextSize(25);
textView.setGravity(Gravity.CENTER);
return textView;
}
/**
* 绑定数据
*/
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//设置内容
textView.setText(content);
}
}
- Adapted
package com.example.dell.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
/**
* Created by dell on 2020/2/4.
*/
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<MyFragment> fragments;
public ViewPagerAdapter(FragmentManager fm, ArrayList<MyFragment> fragments) {
super(fm);
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 fragments.get(position).getTitle();
}
}
<?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=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_bright"
app:tabIndicatorColor="@android:color/background_dark"
app:tabSelectedTextColor="@android:color/background_dark"
app:tabTextColor="@android:color/white" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>