安卓Vapage2实现底部导航菜单

java 复制代码
package com.zx.tab;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final int[] tabIds = {
            R.id.ll_home,
            R.id.ll_mine
    };
    private final int[] iconIds = {
            R.id.im_home,
            R.id.im_mine
    };
    private final int[] textIds = {
            R.id.tv_home,
            R.id.tv_mine
    };
    private ViewPager2 viewPager2=null;
    private ImageView[] tabIcons=null;
    private TextView[] tabTexts=null;
    private ImageView imCurrent=null;
    private TextView tvCurrent=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.initializeViews();
        this.initViewPager();
        this.changeTab(0);

    }
    private void initializeViews() {

        this.viewPager2 = findViewById(R.id.viewPager2);

        LinearLayout[] tabLayouts = new LinearLayout[2];
        this.tabIcons = new ImageView[2];
        this. tabTexts = new TextView[2];

        for (int i = 0; i < 2; i++) {
            tabLayouts[i] = findViewById(tabIds[i]);
            tabIcons[i] = findViewById(iconIds[i]);
            tabTexts[i] = findViewById(textIds[i]);
            final int index = i;
            tabLayouts[i].setOnClickListener(view -> {
                viewPager2.setCurrentItem(index, false);
                changeTab(index);
            });
        }

    }
    private void initViewPager() {
    this. fragmentList.add(ClientHomeNewFragment.newInstance(getString(R.string.tv_home)));
        this.fragmentList.add(ClientMeFragment.newInstance(getString(R.string.tv_mine)));
        this.viewPager2.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList));
        this. viewPager2.setUserInputEnabled(false);//是否滑动
        this.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                changeTab(position);
            }
        });
    }
    private void changeTab(int position) {
        if (imCurrent != null && tvCurrent != null) {
            this.imCurrent.setSelected(false);
            this.tvCurrent.setSelected(false);
        }
        this.imCurrent = tabIcons[position];
        this.tvCurrent = tabTexts[position];
        this.imCurrent.setSelected(true);
        this.tvCurrent.setSelected(true);
    }
}

Fragment适配器

java 复制代码
package com.zx.tab;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.List;

/****
 * 用于管理Fragment。
 */
public class FragmentAdapter extends FragmentStateAdapter {

    private List<Fragment> fragmentList;

    /**
     * Instantiates a new Fragment adapter.
     * @param fragmentManager
     *         the fragment manager
     * @param lifecycle
     *         the lifecycle
     * @param fragments
     *         the fragments
     */
    public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
        super(fragmentManager, lifecycle);

        fragmentList = fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {

        return fragmentList.get(position);
    }
    @Override
    public int getItemCount() {
        if (fragmentList!= null && !fragmentList.isEmpty()) {
            return fragmentList.size();
        } else {
            return 0 ;
        }
    }
    }

下面是fragment,2个我我这点给出一一个,另外一个都是一样的

java 复制代码
package com.zx.tab;

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;

public class ClientHomeNewFragment extends Fragment {

    private static final String ARG_PARAM1 = "首页";

    public static ClientHomeNewFragment newInstance(String param1) {
        ClientHomeNewFragment quoteFragment = new ClientHomeNewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        quoteFragment.setArguments(args);
        return quoteFragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.home, container, false);
        return contentView;
    }
}

下面是布局

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"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#f0f0f0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/white"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/ll_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/im_home"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="@drawable/tab_menu_home" />

            <TextView
                android:id="@+id/tv_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tv_home"
                android:textColor="@drawable/tabar_title_text"
                android:textSize="12sp" />
        </LinearLayout>



        <LinearLayout
            android:id="@+id/ll_mine"
            android:layout_width="0dp"
            android:layout_height="56dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/im_mine"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:background="@drawable/tab_menu_mine" />

            <TextView
                android:id="@+id/tv_mine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tv_mine"
                android:textColor="@drawable/tabar_title_text"
                android:textSize="12sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

效果图

详情请见详情案例
https://download.csdn.net/download/qq_41733851/88879647?spm=1001.2101.3001.9500

相关推荐
开心呆哥5 分钟前
【如何使用 ADB 脚本批量停止 Android 设备上的所有应用】
android·adb
CYRUS STUDIO1 小时前
ARM64汇编寻址、汇编指令、指令编码方式
android·汇编·arm开发·arm·arm64
weixin_449310842 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
Zender Han2 小时前
Flutter自定义矩形进度条实现详解
android·flutter·ios
白乐天_n4 小时前
adb:Android调试桥
android·adb
姑苏风8 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k11 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小1012 小时前
JavaWeb项目-----博客系统
android
风和先行12 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.13 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list