Android之Tab页面切换

Android之Tab页面切换

1. 实现效果

在这里插入图片描述

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;

/**
 * @author zyy
 * @date 2021年05月25日 上午9:51
 */
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<>();
        //1
        TabTittleList.add(TabTitle[0]);
        TabFragmentList.add(new DingWeiBiao_GuanLi());
        //2
        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>

    
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值