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

相关推荐
ChinaDragonDreamer2 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
网络研究院4 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
凉亭下4 小时前
android navigation 用法详细使用
android
小比卡丘7 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
前行的小黑炭8 小时前
一篇搞定Android 实现扫码支付:如何对接海外的第三方支付;项目中的真实经验分享;如何高效对接,高效开发
android
落落落sss9 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.10 小时前
数据库语句优化
android·数据库·adb
GEEKVIP12 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model200514 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏68914 小时前
Android广播
android·java·开发语言