Android实现底部菜单的跳转--BottomNavigationView

效果图:

Android实现底部菜单的跳转--BottomNavigationView_第1张图片Android实现底部菜单的跳转--BottomNavigationView_第2张图片Android实现底部菜单的跳转--BottomNavigationView_第3张图片

1.新建一个一个Activity

Android实现底部菜单的跳转--BottomNavigationView_第4张图片

2.在分别新建三个Activity,放置底部按钮对应的界面

Sub1Activity.java如下:其余Activity.java文件对照于此

package com.huilian.manager;

import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Sub1Activity extends Fragment {
    private TextView textView;
    private Button button;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //不同的Activity对应不同的布局
        View view = inflater.inflate(R.layout.activity_sub1, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /**
         * TODO 实现底部菜单对应布局控件事件
         * */

    }

}

3.MainActivity.java为主要部分,设计如下:

package com.huilian.manager;


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.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private Fragment fragment1;
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment[] fragments;
    private int lastfragment;//用于记录上个选择的Fragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // listView = (ListView) findViewById(R.id.mainList);
        BottomNavigationView navigation = findViewById(R.id.navigation1);
        initFragment();
    }
    private void initFragment() {
        fragment1 = new Sub1Activity();
        fragment2 = new Sub2Activity();
        fragment3 = new Sub3Activity();
        fragments = new Fragment[]{fragment1, fragment2, fragment3};
        lastfragment = 0;
        getSupportFragmentManager().beginTransaction().replace(R.id.mainView, fragment1).show(fragment1).commit();
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation1);

        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home: {
                    if (lastfragment != 0) {
                        switchFragment(lastfragment, 0);
                        lastfragment = 0;
                    }
                    return true;
                }
                case R.id.navigation_dashboard: {
                    if (lastfragment != 1) {
                        switchFragment(lastfragment, 1);
                        lastfragment = 1;

                    }

                    return true;
                }
                case R.id.navigation_notifications: {
                    if (lastfragment != 2) {
                        switchFragment(lastfragment, 2);
                        lastfragment = 2;
                    }
                    return true;
                }
            }
            return false;
        }
    };

    //切换Fragment
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.mainView, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

}

4.activity_main.xml如下:其中 app:menu="@menu/navigation" 在项目的res资源文件下menu菜单下navigation.xml作为显示底部菜单属性的设置,同时里的每一个菜单的ID也将作为MainActivity.java 连接使用跳转不同页面依据。




    
    

    

5.最终项目结构效果

Android实现底部菜单的跳转--BottomNavigationView_第5张图片

6.AndroidManifest.xml




    
        
            
                

                
            
        
        
        
        
    

 

 

 

你可能感兴趣的:(Android实现底部菜单的跳转--BottomNavigationView)