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

相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen3 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone3 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui