最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
android开发中BottomNavigationBar使用例子
时间:2016-04-17 编辑:简简单单 来源:一聚教程网
这次写个新的底部栏实现方式,BottomNavigationBar,貌似上个月谷歌发布的,使用起来也很方便下面简单介绍下使用方式,
1、compile ‘com.ashokvarma.android:bottom-navigation-bar:0.9.5’ 在gradle中加入这句
2、布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context="com.example.chenxu.mybottomdemo.MainActivity">
android:id="@+id/ll_root"
android:orientation="vertical"
android:layout_
android:layout_
android:layout_above="@+id/bottom_bar"
/>
android:id="@+id/bottom_bar"
android:layout_
android:layout_
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即可,以上就是全部内容,用起来效果不错,推荐使用
-
上一个: iOS创建简单的菜单的例子
相关文章
- Android开发控制ScrollView滑动速度的方法 04-01
- Android开发中ListView嵌套GridView问题详解 08-25
- Android开发之Retrofit用法详解 08-15
- Android开发之实现毛玻璃模糊效果 04-25
- Android开发中EditText只能输入金额 01-21
- Android开发中的Fragment,帧动画,补间动画,属性动画介绍 11-12