安卓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

相关推荐
还鮟1 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡2 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi003 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil4 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你4 小时前
Android View的绘制原理详解
android
移动开发者1号7 小时前
使用 Android App Bundle 极致压缩应用体积
android·kotlin
移动开发者1号7 小时前
构建高可用线上性能监控体系:从原理到实战
android·kotlin
ii_best12 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk12 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭17 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin