最简单的android底部导航栏 + Fragment的实现方式

1、android.support.design.widget.BottomNavigationView 是android原生的一个底部导航框架

区别于gitthub上start数量很多的com.roughike:bottom-bar:2.3.1'(github地址:点击打开链接):

①android原生的bottomNavigationView初始代码很少也很简单,掌握起来也很快。

②在不使用ViewPager实现联动的情况下,bottomNavigationView使用起来还是很简单,容易上手的。(如有与ViewPager联动的需求,建议使用bottombar)


2、bottomNavigationView + Fragment的实现方式过程:

一、新建项目以BottomNavigationView为初始化模板,默认代码如下:

①activity_main.xml




    

    

最初的xml代码中仅有两个基本控件,一个是tab切换的时候显示的textView,还有一个就是BottomNavigationView,其中决定tab底部按钮的显示的,就是menu文件夹下的navigation.xml文件。

②navigation.xml




    

    

    

③MainActivity.java


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

显然,点击tab切换页面的逻辑是发生在OnNavigationItemSelectedListener这个回调中,当然,我们的实际需求肯定不是为了切换显示一个TextView,那也太没有意义了,接下来就是怎么把Fragment中替换到原来的TextView中。

二、添加Fragment

1、新建几个Fragment,切换嘛,没有几个Fragment怎么切换。新建Fragment1、Fragment2、Fragment3等。

2、


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

public class MainActivity extends FragmentActivity {
    private Fragment1 fragment1;     //Fragment1对象
    private Fragment2 fragment2;     //Fragment2对象
    private Fragment[] fragments;    //Fragment数组
    private int lastShowFragment = 0;   //表示最后一个显示的Fragment

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (lastShowFragment != 0) {
                        switchFrament(lastShowFragment, 0);
                        lastShowFragment = 0;
                    }
                    return true;
                case R.id.navigation_dashboard:
                    if (lastShowFragment != 1) {
                        switchFrament(lastShowFragment, 1);
                        lastShowFragment = 1;
                    }
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        initFragments();
    }

    /**
     * 切换Fragment
     *
     * @param lastIndex 上个显示Fragment的索引
     * @param index     需要显示的Fragment的索引
     */
    public void switchFrament(int lastIndex, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastIndex]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.container, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }
    private void initFragments() {
        fragment1 = new PstationFragment();
        fragment2 = new UserInfoFragment();
        fragments = new Fragment[]{fragment1, fragment2};
        lastShowFragment = 0;
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, fragment1)
                .show(fragment1)
                .commit();
    }
}

你可能感兴趣的:(android,View系列)