0
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0' // ViewPager 2 需要使用 RecycleView 的 adapter
implementation 'com.google.android.material:material:1.0.0'
1引入布局
app:tabMode=“auto” 表示自适应,好用
app:tabMode=“scrollable” 表示滚动,不好用,超过一屏会均分;不超过一屏不均分
app:tabSelectedTextColor="#0371DD" 条目选中时的字体颜色,蓝
app:tabTextColor="#111111" 条目未选中时的字体颜色,黑
app:tabIndicatorColor="#0371DD" tab指示器颜色,蓝
app:tabTextAppearance="@android:style/TextAppearance.Holo.Large" 改变tab字体大小
app:tabIndicatorHeight=“5dp” 改变指示器高度;为0则没有指示器
app:tabContentStart=“100dp” 整个tab距离开始布局的位置的值
<?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">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tb_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="auto"
app:tabIndicatorColor="#0371DD"
app:tabSelectedTextColor="#0371DD"
app:tabTextColor="#111111" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_tb"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
tab代码中动态添加 文字
tabLayout.addTab(tabLayout.newTab().setText(“第六个界面”));
添加文字和图片
tabLayout.addTab(tabLayout.newTab().setText(“Tab 1”).setIcon(R.mipmap.ic_launcher));
tabLayout联动viewPager,通过适配器TabAdapter进行关联,TabAdapter关联fragment
TabAdapter adapter = new TabAdapter(this);
viewPager2 = findViewById(R.id.vp_tb);
tabLayout = findViewById(R.id.tb_vp);
viewPager2.setAdapter(adapter);
全代码
package com.example.myapplication.tab;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import com.example.myapplication.R;
import com.google.android.material.tabs.TabLayout;
public class TabActivity extends AppCompatActivity {
private ViewPager2 viewPager2;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabAdapter adapter = new TabAdapter(this);
viewPager2 = findViewById(R.id.vp_tb);
tabLayout = findViewById(R.id.tb_vp);
viewPager2.setAdapter(adapter);
//动态控制字体颜色
// tabLayout.setTabTextColors(Color.parseColor("#111111"),Color.parseColor("#0371DD"));
adapter.addColor(android.R.color.darker_gray);
adapter.addColor(android.R.color.holo_red_dark);
adapter.addColor(android.R.color.holo_green_dark);
adapter.addColor(android.R.color.holo_blue_dark);
adapter.addColor(android.R.color.holo_purple);
adapter.addColor(android.R.color.holo_orange_dark);
tabLayout.addTab(tabLayout.newTab().setText("第一个界面"));
tabLayout.addTab(tabLayout.newTab().setText("第二个界面"));
tabLayout.addTab(tabLayout.newTab().setText("第三个界面"));
tabLayout.addTab(tabLayout.newTab().setText("第四个界面"));
tabLayout.addTab(tabLayout.newTab().setText("第五个界面"));
tabLayout.addTab(tabLayout.newTab().setText("第六个界面"));
//添加图标
tabLayout.addTab(tabLayout.newTab().setText("Tab 1").setIcon(R.mipmap.ic_launcher_round));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
Log.e("wy","======我选中了===="+tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
Log.e("wy","======未被选中===="+tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Log.e("wy","======再次被选中===="+tab.getPosition());
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
tabLayout.setScrollPosition(position, 0, false);
Log.e("wy", "onPageSelected position: "+position );
}
});
}
}
布局
<?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">
<TextView
android:id="@+id/title_bart"
android:text="@string/msg"
android:textColor="@color/white"
android:textSize="17sp"
android:gravity="center"
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="83dp" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tb_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="#0371DD"
app:tabSelectedTextColor="#0371DD"
app:tabTextColor="#111111" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_tb"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ShowFragment 在此!!
package com.example.myapplication.tab;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Use the {@link ShowFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class ShowFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_show, container, false);
}
static ShowFragment newInstance(List<Integer> colors, int item) {
Bundle bundle = new Bundle();
bundle.putSerializable("color", (ArrayList<Integer>) colors);
bundle.putInt("item", item);
ShowFragment fragment = new ShowFragment();
fragment.setArguments(bundle);
return fragment;
}
@SuppressLint("SetTextI18n")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
view.<FrameLayout>findViewById(R.id.fl_show)
.setBackgroundResource(((ArrayList<Integer>) getArguments()
.getSerializable("color")).get(getArguments().getInt("item")));
view.<TextView>findViewById(R.id.tv_show).setText("第 " + (getArguments().getInt("item") + 1) + " 个页面");
super.onViewCreated(view, savedInstanceState);
}
}
fragment_show.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tab.ShowFragment">
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment"
android:textColor="#ffffff" />
</FrameLayout>