android基础控件(3)BottomNavigationView+FragmentLayout实现底层导航栏

1.BottomNavigationView+FragmentLayout

1.1 activity_main

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".base.MainActivity">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="40dp"
    android:id="@+id/frag1"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bnv1"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    app:menu="@menu/bottomnavigationmenu"
    android:background="@color/white"/>
RelativeLayout>
1.2 BottomNavigationView添加menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/home"
       android:title="主页"
       android:icon="@mipmap/home_normal"/>
    <item android:id="@+id/select"
        android:title="精选"
        android:icon="@mipmap/select_normal"/>
    <item android:id="@+id/red_packet"
        android:title="特惠"
        android:icon="@mipmap/red_packet_normal"/>
    <item android:id="@+id/search"
        android:title="搜索"
        android:icon="@mipmap/search_normal"/>
menu>

#####1.3 Main_Activity

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView mBottomNavigationView;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;
    private HomeFragment mHomeFragment;
    private  static Context mContext;
    private SelectFragment mSelectFragment;
    private RedFragment mRedFragment;
    private SearchFragment mSearchFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        initListener();
        mContext=getContext();
    }
    public static Context getContext(){
        return mContext;
    }
    private void initListener() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.home:
                        switchFragment(mHomeFragment);
                        break;
                    case R.id.select:
                        switchFragment(mSelectFragment);
                        break;
                    case R.id.red_packet:
                        switchFragment(mRedFragment);
                        break;
                    case R.id.search:
                        switchFragment(mSearchFragment);
                        break;
                }
                return true;
            }
        });
    }
    private Fragment lastFramgent=null;
    private void switchFragment(Fragment homeFragment) {
        mTransaction = mFragmentManager.beginTransaction();
        if (!homeFragment.isAdded()) {
            mTransaction.add(R.id.frag1,homeFragment);
        }else {
            mTransaction.show(homeFragment);
        }
        if (lastFramgent!=null){
            mTransaction.hide(lastFramgent);
        }
        lastFramgent=homeFragment;
//        mTransaction.replace(R.id.frag1,homeFragment);
        mTransaction.commit();
    }
    private void initUI() {
        mBottomNavigationView = findViewById(R.id.bnv1);
        mFragmentManager = getSupportFragmentManager();
        mHomeFragment = new HomeFragment();
        mSelectFragment = new SelectFragment();
        mRedFragment = new RedFragment();
        mSearchFragment = new SearchFragment();
        switchFragment(mHomeFragment);
    }
}
1.4为每个menu添加Fragment

每个Fragment实现对应的功能,这里省略详细的布局。

你可能感兴趣的:(android开发,android,java,android,studio)