android framework开发_android开发中BottomNavigationBar使用例子

更新时间:2020-09-11    来源:.Net开发    手机版     字体:

【www.bbyears.com--.Net开发】

这次写个新的底部栏实现方式,BottomNavigationBar,貌似上个月谷歌发布的,使用起来也很方便下面简单介绍下使用方式,

1、compile ‘com.ashokvarma.android:bottom-navigation-bar:0.9.5’     在gradle中加入这句

2、布局文件



    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.chenxu.mybottomdemo.MainActivity">
    android:id="@+id/ll_root"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom_bar"
    />
            android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true" />
 

3、Java代码


package com.example.chenxu.mybottomdemo;
 
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
 
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
 
 
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
    private BottomNavigationBar mBottomBar;
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;
    private FragmentThree fragmentThree;
    private FragmentFore fragmentFore;
    private FragmentFive fragmentFive;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();
    }
 
    /**
     * 添加页面
     */
    private void assignViews() {
        mBottomBar = (BottomNavigationBar) findViewById(R.id.bottom_bar);
        mBottomBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "商品"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "分类"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "购物车"))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "我的"))
                .initialise();
        mBottomBar.setTabSelectedListener(this);//设置监听
        setDefaultFragment();//设置默认选项
    }
    private void setDefaultFragment() {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        fragmentOne=new FragmentOne();
        fragmentTransaction.add(R.id.ll_root,fragmentOne);
        fragmentTransaction.commit();
    }
 
    /**
     * 隐藏fragment
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (fragmentOne != null) {
            transaction.hide(fragmentOne);
        }
        if (fragmentTwo != null) {
            transaction.hide(fragmentTwo);
        }
        if (fragmentThree != null) {
            transaction.hide(fragmentThree);
        }
        if (fragmentFore != null) {
            transaction.hide(fragmentFore);
        }
        if (fragmentFive != null) {
            transaction.hide(fragmentFive);
        }
    }
 
    @Override
    public void onTabSelected(int position) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (position){
            case 0:
                if (fragmentOne==null){
                    fragmentOne=new FragmentOne();
                    fragmentTransaction.add(R.id.ll_root,fragmentOne);
 
                }else {
                    fragmentTransaction.show(fragmentOne);
                }
                break;
            case 1:
                if (fragmentTwo==null){
                    fragmentTwo=new FragmentTwo();
                    fragmentTransaction.add(R.id.ll_root,fragmentTwo);
 
                }else {
                    fragmentTransaction.show(fragmentTwo);
                }
                break;
            case 2:
                if (fragmentThree==null){
                    fragmentThree=new FragmentThree();
                    fragmentTransaction.add(R.id.ll_root,fragmentThree);
 
                }else {
                    fragmentTransaction.show(fragmentThree);
                }
                break;
            case 3:
                if (fragmentFore==null){
                    fragmentFore=new FragmentFore();
                    fragmentTransaction.add(R.id.ll_root,fragmentFore);
 
                }else {
                    fragmentTransaction.show(fragmentFore);
                }
                break;
            case 4:
                if (fragmentFive==null){
                    fragmentFive=new FragmentFive();
                    fragmentTransaction.add(R.id.ll_root,fragmentFive);
 
                }else {
                    fragmentTransaction.show(fragmentFive);
                }
                break;
        }
        fragmentTransaction.commit();
    }
 
    @Override
    public void onTabUnselected(int position) {
 
    }
 
    @Override
    public void onTabReselected(int position) {
 
    }
}

注:每个底部按钮对应一个fragment即可,以上就是全部内容,用起来效果不错,推荐使用

本文来源:http://www.bbyears.com/asp/98091.html

热门标签

更多>>

本类排行