android BottomNavigationView+FrameLayout 实现切换fragment

昨天发现使用Google的BottomNavigationView重写了OnNavigationItemSelectedListener之后切换不了fragment在查询了半天资料无果后,发现了一种区别去viewpager的方法

在这里记录一下

使用fragment一般有两种方式 一种是静态添加,在布局文件中加fragment 第二种就是动态添加fragment

android自动生成的BottomNavigationView方法就是使用了第一种方式

和BottomNavigationView类似,但是将布局文件中的静态设置fragment改为了动态设置fragment,废话少说,直接上代码

布局文件:

<?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/title_layout_blue"
       />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_below="@+id/total1"
        android:id="@+id/scrollview1"
        android:layout_weight="1">
        <com.handset.lane.widget.ShadowLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    <FrameLayout
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

            </LinearLayout>

        </com.handset.lane.widget.ShadowLayout>
    </ScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu"
        />

</LinearLayout>

activity:

package com.handset.lane.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.handset.lane.R;
import com.handset.lane.activity.ui.cpc.HomeFragment;
import com.handset.lane.activity.ui.obu.DashboardFragment;
import com.handset.lane.base.BaseActivity;

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

public class CardInfoActivity extends AppCompatActivity {

    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.img_back)
    LinearLayout back;
    private FragmentManager mSupportFragmentManager;
    private FragmentTransaction mTransaction;
    private List<Fragment> mFragments = new ArrayList<>();
    private HomeFragment homeFragment;
    private DashboardFragment dashboardFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_info);
        this.getSupportActionBar().hide();
        ButterKnife.bind(this);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        TextView title = findViewById(R.id.tv_title);
        title.setText(R.string.cpc);
        initView();
        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        title.setText(R.string.cpc);
                        hide(homeFragment, false);
                        return true;

                    case R.id.navigation_dashboard:
                        title.setText(R.string.obu);
                        if (dashboardFragment == null) {
                            dashboardFragment = new DashboardFragment();
                            mFragments.add(dashboardFragment);
                            hide(dashboardFragment, true);
                        } else {
                            hide(dashboardFragment, false);
                        }

                        return true;
                }
                return false;
            }
        });

        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initView() {
        mSupportFragmentManager = getSupportFragmentManager();
        mTransaction = mSupportFragmentManager.beginTransaction();
        homeFragment = new HomeFragment();
        mFragments.add(homeFragment);
        hide(homeFragment, true);

    }

    private void hide(Fragment fragment, boolean add) {
        mTransaction = mSupportFragmentManager.beginTransaction();
        if (add) {
            mTransaction.add(R.id.nav_host_fragment, fragment);
        }

        for (Fragment fragments : mFragments) {
            if (fragment.equals(fragments)) {
                mTransaction.show(fragments);
            } else {
                mTransaction.hide(fragments);
            }
        }
        mTransaction.commit();


    }

}

动态添加fragment常用的类:

FragmentManager:用来管理Activity中的fragment,app包中使用getFragmentManager()   v4包中getSupportFragmentManager

FragmentTransaction:事务,用来添加,移除,替换fragment,FragmentTransaction transaction = fm.benginTransatcion();//开启一个事务

然后使用事务,在Activity中动态添加,隐藏fragment,在OnNavigationItemSelectedListener调用方法实现隐藏,显示

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值