底部导航BottomNavigationBar使用
首先 惯例 导包
implementation 'com.ashokvarma.android:bottom-navigation-bar:2.0.4'
然后 简单粗暴 贴代码
1、MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/main_layFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bnb_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"/>
</LinearLayout>
2、MainActivity主界面
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
private ArrayList<Fragment> fragments;
private Home1Fragment home1Fragment;
private Home2Fragment home2Fragment;
private Home3Fragment home3Fragment;
private Home4Fragment home4Fragment;
private BottomNavigationBar bottomNavigationBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bnb_main);
/**
* MODE_DEFAULT item<3为MODE_FIXED,>3为MODE_SHIFTING
* MODE_FIXED 图标固定大小
* MODE_SHIFTING 图标不固定大小
*/
bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
/**
*
* BACKGROUND_STYLE_DEFAULT MODE_FIXED--->BACKGROUND_STYLE_STATIC,MODE_SHIFTING--->BACKGROUND_STYLE_RIPPLE
* BACKGROUND_STYLE_STATIC 点击时不带水波纹效果
* BACKGROUND_STYLE_RIPPLE 点击时带水波纹效果
*/
bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
bottomNavigationBar.
addItem(new BottomNavigationItem(R.mipmap.base_home1, "页面1")//选中时图标
.setActiveColorResource(R.color.black)//选中颜色
.setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.mipmap.base_home1_nomal)))//未选中时图标
.addItem(new BottomNavigationItem(R.mipmap.base_home2, "页面2")
.setActiveColorResource(R.color.black)
.setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.mipmap.base_home2_nomal)))
.addItem(new BottomNavigationItem(R.mipmap.base_home3, "页面3")
.setActiveColorResource(R.color.black)
.setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.mipmap.base_home3_nomal)))
.addItem(new BottomNavigationItem(R.mipmap.base_home4, "页面4")
.setActiveColorResource(R.color.black)
.setInactiveIcon(ContextCompat.getDrawable(MainActivity.this, R.mipmap.base_home4_nomal)))
.setFirstSelectedPosition(0)//初次进入时页面
.initialise();
fragments = getFragments();
setDefaultFragment();
bottomNavigationBar.setTabSelectedListener(this);
}
private void setDefaultFragment() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.main_layFrame, home1Fragment);
transaction.add(R.id.main_layFrame, home2Fragment);
transaction.hide(home2Fragment);
transaction.add(R.id.main_layFrame, home3Fragment);
transaction.hide(home3Fragment);
transaction.add(R.id.main_layFrame, home4Fragment);
transaction.hide(home4Fragment);
transaction.commit();
}
private ArrayList<Fragment> getFragments() {
ArrayList<Fragment> fragments = new ArrayList<>();
home1Fragment = Home1Fragment.newInstance("页面1");
fragments.add(home1Fragment);
home2Fragment = Home2Fragment.newInstance("页面2");
fragments.add(home2Fragment);
home3Fragment = Home3Fragment.newInstance("页面3");
fragments.add(home3Fragment);
home4Fragment = Home4Fragment.newInstance("页面4");
fragments.add(home4Fragment);
return fragments;
}
//未选中 ---> 选中
@Override
public void onTabSelected(int position) {
if (fragments != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
switch (position) {
case 0:
ft.show(home1Fragment);
ft.hide(home2Fragment);
ft.hide(home3Fragment);
ft.hide(home4Fragment);
break;
case 1:
ft.show(home2Fragment);
ft.hide(home1Fragment);
ft.hide(home3Fragment);
ft.hide(home4Fragment);
break;
case 2:
ft.show(home3Fragment);
ft.hide(home1Fragment);
ft.hide(home2Fragment);
ft.hide(home4Fragment);
break;
case 3:
ft.show(home4Fragment);
ft.hide(home1Fragment);
ft.hide(home2Fragment);
ft.hide(home3Fragment);
break;
default:
break;
}
ft.commit();
}
}
//选中 ---> 未选中
@Override
public void onTabUnselected(int position) {
if (fragments != null) {
}
}
//选中 ---> 选中
@Override
public void onTabReselected(int position) {
}
}
3、Home1Fragment界面加上这一段
public static Home1Fragment newInstance(String content) {
Bundle args = new Bundle();
args.putString("ARGS", content);
Home1Fragment fragment = new Home1Fragment();
fragment.setArguments(args);
return fragment;
}
收工。