Android之Tab页面切换
1. 实现效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cb45b7a019659faf23d4e0c2cb777c8b.gif)
2. 代码实现:
- 一个TabHost
- 两个Fragment
- 一个界面
package cn.app.project.tableshow.home.huo_ti_zi_chan_guan_li.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
import cn.app.project.tableshow.R;
import cn.app.project.tableshow.home.huo_ti_zi_chan_guan_li.adapter.TabFragmentAdapter;
import cn.app.project.tableshow.view.FixedTopView;
public class DingWeiBiaoGuanLiAll extends Fragment {
private FixedTopView fixedTopView;
private ViewPager main_frag;
private TabLayout tabLayout;
private String [] TabTitle={
"定位标管理","记录管理"};
private List<TabFragment> TabFragmentList;
private List<String> TabTittleList;
private TabFragmentAdapter tabFragmentAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.fragment_huo_ti_zi_chan_guan_li, container, false);
initView(root);
initData();
return root;
}
private void initView(View root){
fixedTopView = root.findViewById(R.id.huo_ti_zi_chan_guan_li_fixedTopView);
NavHostFragment navHostFragment =(NavHostFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
fixedTopView.setNavController(navHostFragment.getNavController());
main_frag=(ViewPager)root.findViewById(R.id.huo_ti_zi_chan_guan_li_main_frag);
tabLayout=(TabLayout)root.findViewById(R.id.huo_ti_zi_chan_guan_li_TopNav);
}
private void initData(){
TabFragmentList=new ArrayList<>();
TabTittleList=new ArrayList<>();
TabTittleList.add(TabTitle[0]);
TabFragmentList.add(new DingWeiBiao_GuanLi());
TabTittleList.add(TabTitle[1]);
TabFragmentList.add(new DingWeiBiao_JiLuGuanLi());
tabFragmentAdapter=new TabFragmentAdapter(getChildFragmentManager(),TabFragmentList,TabTittleList);
main_frag.setAdapter(tabFragmentAdapter);
tabLayout.setupWithViewPager(main_frag);
}
}
<?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">
<cn.app.project.tableshow.view.FixedTopView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:top_title="定位标管理"
android:id="@+id/huo_ti_zi_chan_guan_li_fixedTopView"/>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="50dp"
style="@style/MyTablayoutstyle"
android:id="@+id/huo_ti_zi_chan_guan_li_TopNav"/>
<androidx.viewpager.widget.ViewPager
style="@style/myViewPaper"
android:id="@+id/huo_ti_zi_chan_guan_li_main_frag"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTablayoutstyle" parent="Base.Widget.Design.TabLayout">
<item name="tabBackground">@color/white</item>
<item name="tabMode">fixed</item>
<item name="tabIndicatorColor">@color/current_select_main_time</item>
<item name="tabIndicatorHeight">2dp</item>
<item name="tabSelectedTextColor">@color/current_select_main_time</item>
<item name="tabTextColor">@color/black</item>
</style>