大四寒假日期汇报1.13

使用BottomNavigationView组件完成底部导航栏

在AndroidStudio上新建activity时,有一个默认带有BottomNavigationView组件的activity,同时完成了一些初始化的一些操作。所以这里直接使用这些默认的代码,在此基础上我们使用FragmentTransaction完成对于Fragment的切换,

activity_index.java代码

package sportwin.heiyang.com.sportwinner.HomeAcitvity;

import android.app.Activity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;


import sportwin.heiyang.com.sportwinner.R;

public class IndexActivity extends AppCompatActivity implements Sports_communityFragment.OnFragmentInteractionListener,FindFragment.OnFragmentInteractionListener,SportFragment.OnFragmentInteractionListener,MyFragment.OnFragmentInteractionListener {
    private BottomNavigationView navigation;

    private FragmentManager fManager;

    private Fragment fragment_find;
    private Fragment fragment_sport;
    private Fragment fragment_sport_community;
    private Fragment fragment_my;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        //设置一些系统参数
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        setStatusBar();
        //设置任务栏图标黑色
        changStatusIconCollor(true);
        //设置fragment
        intiFragment();

        navigation = findViewById(R.id.navigation);
        SetItemSelectedListener();


    }


    //设置底部导航按钮的监听器
    private void SetItemSelectedListener() {
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                fManager = getSupportFragmentManager();
                FragmentTransaction fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                //开始切换fragment
                System.out.println("开始切换Fragment");
                switch (item.getItemId()) {
                    case R.id.navigation_sports_community:
                        if(fragment_sport_community == null){
                            fragment_sport_community = new Sports_communityFragment();
                            fTransaction.add(R.id.index_content,fragment_sport_community).commit();
                            System.out.println("添加展示运动圈页面");
                        }else{
                            fTransaction.show(fragment_sport_community).commit();
                            System.out.println("展示运动圈页面");
                        }
                        return true;
                    case R.id.navigation_find:
                        if(fragment_find == null){
                            fragment_find = new FindFragment();
                            fTransaction.add(R.id.index_content,fragment_find).commit();
                            System.out.println("添加展示发现页面");
                        }else{
                            fTransaction.show(fragment_find).commit();
                            System.out.println("展示发现页面");
                        }
                        return true;
                    case R.id.navigation_sport:
                        if(fragment_sport == null){
                            fragment_sport = new SportFragment();
                            fTransaction.add(R.id.index_content,fragment_sport).commit();
                            System.out.println("添加展示运动页面");
                        }else{
                            fTransaction.show(fragment_sport).commit();
                            System.out.println("展示运动页面");
                        }
                        return true;
                    case R.id.navigation_my:
                        if(fragment_my == null){
                            fragment_my = new MyFragment();
                            fTransaction.add(R.id.index_content,fragment_my).commit();
                            System.out.println("添加展示我的页面");
                        }else{
                            fTransaction.show(fragment_my).commit();
                            System.out.println("展示我的页面");
                        }
                        return true;
                }
                return false;
            }
        });
    }


    //初始化Fragment
    private void intiFragment() {
        fragment_sport_community=new Sports_communityFragment();
        //开始初始化第一个fragment
        System.out.println("开始初始化第一个fragment");
        fManager = getSupportFragmentManager();
        FragmentTransaction fTransaction = fManager.beginTransaction();
        fTransaction.replace(R.id.index_content,fragment_sport_community);
        fTransaction.commit();
    }

    //隐藏所有fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){

        if(fragment_sport_community != null)fragmentTransaction.hide(fragment_sport_community);
        if(fragment_find != null)fragmentTransaction.hide(fragment_find);
        if(fragment_sport != null)fragmentTransaction.hide(fragment_sport);
        if(fragment_my != null)fragmentTransaction.hide(fragment_my);
    }



//    设置任务栏图标颜色为黑色
    public void changStatusIconCollor(boolean setDark) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            View decorView = getWindow().getDecorView();
            if(decorView != null){
                int vis = decorView.getSystemUiVisibility();
                if(setDark){
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else{
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

这里面四个Fragment的代码与布局不再展示,原因是自己写的十分粗糙,仅仅是实现了效果,你可以自己实现来进行替代,不会出现技术上问题。

需要注意的是,FragmentTransaction 每次使用的时候需要重新初始化,不能一次初始化后多次反复使用。

FragmentTransaction fTransaction = fManager.beginTransaction();

activity_index.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    tools:context=".HomeAcitvity.IndexActivity">


    <FrameLayout
        android:id="@+id/index_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:orientation="horizontal">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:background="?android:attr/windowBackground"
        app:itemIconTint="@color/color_state_menu_navi"
        app:itemTextColor="@color/color_state_menu_navi"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</RelativeLayout >

xml代码中app:menu="@menu/navigation"对应文件在你新建那个默认的带有BottomNavigationView组件的activity时会自动创立,然后app:item的两个属性可以删掉,或者查看我往期的博客园可以了解相关信息。

原文地址:https://www.cnblogs.com/heiyang/p/14275389.html