实现功能: 点击不同tab,切换不同fragement,效果图
1. XML布局
<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"
android:fitsSystemWindows="true"
android:theme="@style/customTheme">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:navigationIcon="@drawable/abc_vector_test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="编辑商品"
android:textColor="@color/white"
android:textSize="22sp" />
<TextView
android:id="@+id/good_save"
android:text="保存"
android:textSize="14dp"
android:textColor="@color/white"
android:layout_gravity="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"/>
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabIndicatorColor="#F61010"
app:tabIndicator="@drawable/shape_indicator"
app:tabIndicatorFullWidth="false"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabBackground="@color/white"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp"
app:tabRippleColor="#00000000"
app:tabSelectedTextColor="#333333"
app:tabTextColor="#666666"
app:tabTextAppearance="@style/myTabTextAppearance"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/good_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="80dp"
android:visibility="gone"/>
</LinearLayout>
2. fragement实现 核心代码
public void onActivityCreated(@NonNull Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).statusBarDarkFont(true).init();
tabLayout = mActivity.findViewById(R.id.tabLayout);
mViewPager = mActivity.findViewById(R.id.good_view_pager);
//初始化tab
initTab();
initPage();
}
public void initTab(){
tabLayout.removeAllTabs();
for (int i = 0; i < titles.length; i++) {
TabLayout.Tab tab = tabLayout.newTab();
if (tab != null) {
TextView view = new TextView(mContext);
view.setText(titles[i]);
view.setGravity(Gravity.CENTER);
view.setTextColor(ContextCompat.getColor(mContext, R.color.black));
tab.setCustomView(view);
}
tabLayout.addTab(tab);
}
}
private void initPage() {
fragments.add(GoodItemTitleFragment.newInstance("",""));
fragments.add(GoodItemContentFragment.newInstance("",""));
fragments.add(GoodItemTreeFragment.newInstance("",""));
//tabs.add("全部");
ViewPager2 viewPager2 = mActivity.findViewById(R.id.good_view_pager);
GoodItemFragementAdapter adapter = new GoodItemFragementAdapter(mActivity.getSupportFragmentManager(),getLifecycle(),fragments);
viewPager2.setAdapter(adapter);
new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(titles[position]);
}
}).attach();
}
如遇技术问题,留言反馈。