此选项卡可以配合fragment显示数据 就是之前做的那个
main_activity.java
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tableLayout = findViewById(R.id.tabs);
ViewPager viewPager = findViewById(R.id.vp_main_show);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tableLayout));
tableLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
SectionsPagerAdapter PagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(PagerAdapter);
}
public static class DummyFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
//方法用来返回一个DummyFragment实例
public static DummyFragment newInstance(int sectionNumer) {
DummyFragment fragment = new DummyFragment();
//定义Bundle用于向DummyFragment传入参数
Bundle agrs = new Bundle();
agrs.putInt(ARG_SECTION_NUMBER, sectionNumer);
fragment.setArguments(agrs);
return fragment;
}
//重写该方法用于生成该Fragment的组件
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = rootView.findViewById(R.id.section_label);
textView.setText("页面" + getArguments().getInt(ARG_SECTION_NUMBER, 0));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return DummyFragment.newInstance(i);
}
@Override
public int getCount() {
return 3;
}
}
}
main-xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:tabSelectedTextColor="#4360df">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标签一" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标签二" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标签三" />
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_main_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8" />
</LinearLayout>
fragment-xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/constraintLayout">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
配置dependencies 在bulid.gradle里面
implementation 'com.android.support:design:28.0.0'
同时将targetSdkVersion --defaultConfig { compileSdkVersion--android { 改为28
最终实现
这里再挂一个相关链接
https://blog.youkuaiyun.com/oqqyyyy1/article/details/54310794