|
1 | 1 | package com.base.adev.activity; |
2 | 2 |
|
3 | | -import android.os.Bundle; |
4 | 3 | import android.support.v4.app.Fragment; |
5 | 4 | import android.support.v4.app.FragmentManager; |
6 | 5 | import android.support.v4.app.FragmentTransaction; |
7 | | -import android.support.v7.app.AppCompatActivity; |
8 | 6 |
|
9 | 7 | import com.ashokvarma.bottomnavigation.BottomNavigationBar; |
10 | 8 | import com.ashokvarma.bottomnavigation.BottomNavigationItem; |
|
13 | 11 | import java.util.ArrayList; |
14 | 12 | import java.util.List; |
15 | 13 |
|
16 | | -public abstract class BaseTabBottomActivity extends BaseActivity { |
17 | | - |
18 | | - private BottomBarUtil bottomBarUtil; |
| 14 | +/** |
| 15 | + * 底部 Tab 的基础 Activity |
| 16 | + * 继承此 Activity 后,初始化布局、添加 addItem、initialise 两个方法,传入相关的图标及文字即可。 |
| 17 | + * 想要更换 BottomNavigationBar 风格样式,请设置 setNavBarStyle 方法。 |
| 18 | + */ |
| 19 | +public abstract class BaseTabBottomActivity extends BaseActivity |
| 20 | + implements BottomNavigationBar.OnTabSelectedListener { |
| 21 | + private FragmentManager mFragmentManager; |
| 22 | + private BottomNavigationBar mBottomNavigationBar; |
| 23 | + private final List<Fragment> fragmentList = new ArrayList<>(); |
| 24 | + /** |
| 25 | + * BottomNavigationBar 的风格,默认:MODE_DEFAULT |
| 26 | + */ |
| 27 | + private int mMode = BottomNavigationBar.MODE_DEFAULT; |
| 28 | + /** |
| 29 | + * BottomNavigationBar 的背景样式,默认:BACKGROUND_STYLE_DEFAULT |
| 30 | + */ |
| 31 | + private int mBackgroundStyle = BottomNavigationBar.BACKGROUND_STYLE_DEFAULT; |
19 | 32 |
|
20 | 33 | @Override |
21 | | - protected void initContentView(Bundle bundle) { |
22 | | - setContentView(R.layout.base_activity_tab_bottom); |
23 | | - bottomBarUtil = new BottomBarUtil(this, R.id.llRoot, R.id.bottom_bar); |
| 34 | + protected void initView() { |
| 35 | + mFragmentManager = this.getSupportFragmentManager(); |
| 36 | + mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); |
| 37 | + mBottomNavigationBar.setTabSelectedListener(this); |
24 | 38 | } |
25 | 39 |
|
26 | | - protected void addFragment(Fragment fragment, String title, int imgId) { |
27 | | - bottomBarUtil.addItem(fragment, title, imgId, R.color.colorPrimary); |
| 40 | + /** |
| 41 | + * 添加 fragment 及 BottomNavigationItem(图标和文字) |
| 42 | + * |
| 43 | + * @param fragment fragment |
| 44 | + * @param imageId 图标 ID |
| 45 | + * @param title 标题(String) |
| 46 | + * @param activeColor 选定时颜色 |
| 47 | + */ |
| 48 | + public void addItem(Fragment fragment, int imageId, String title, int activeColor) { |
| 49 | + fragmentList.add(fragment); |
| 50 | + mBottomNavigationBar |
| 51 | + .addItem(new BottomNavigationItem(imageId, title).setActiveColorResource(activeColor)); |
28 | 52 | } |
29 | 53 |
|
30 | | - protected void addFragment(Fragment fragment, int titleId, int imgId) { |
31 | | - bottomBarUtil.addItem(fragment, getResources().getString(titleId), imgId, R.color.colorPrimary); |
| 54 | + /** |
| 55 | + * 添加 fragment 及 BottomNavigationItem(图标和文字) |
| 56 | + * |
| 57 | + * @param fragment fragment |
| 58 | + * @param imageId 图标 ID |
| 59 | + * @param title 标题(int) |
| 60 | + * @param activeColor 选定时颜色 |
| 61 | + */ |
| 62 | + public void addItem(Fragment fragment, int imageId, int title, int activeColor) { |
| 63 | + fragmentList.add(fragment); |
| 64 | + mBottomNavigationBar |
| 65 | + .addItem(new BottomNavigationItem(imageId, title).setActiveColorResource(activeColor)); |
32 | 66 | } |
33 | 67 |
|
34 | | - protected void onTabSelected(int position) { |
35 | | - |
| 68 | + /** |
| 69 | + * 添加 fragment 及 BottomNavigationItem(仅图标) |
| 70 | + * |
| 71 | + * @param fragment fragment |
| 72 | + * @param imageId 图标 ID |
| 73 | + * @param activeColor 选定时颜色 |
| 74 | + */ |
| 75 | + public void addItem(Fragment fragment, int imageId, int activeColor) { |
| 76 | + fragmentList.add(fragment); |
| 77 | + mBottomNavigationBar |
| 78 | + .addItem(new BottomNavigationItem(imageId).setActiveColorResource(activeColor)); |
36 | 79 | } |
37 | 80 |
|
38 | | - protected void initialise() { |
39 | | - bottomBarUtil.initialise(); |
| 81 | + /** |
| 82 | + * 设置 BottomNavigationBar 风格样式 |
| 83 | + * |
| 84 | + * @param mode 风格 |
| 85 | + * @param backgroundStyle 背景样式 |
| 86 | + */ |
| 87 | + protected void setNavBarStyle(int mode, int backgroundStyle) { |
| 88 | + mMode = mode; |
| 89 | + mBackgroundStyle = backgroundStyle; |
40 | 90 | } |
41 | 91 |
|
42 | | - class BottomBarUtil implements BottomNavigationBar.OnTabSelectedListener { |
43 | | - |
44 | | - private int rootViewId; |
45 | | - private FragmentManager fragmentManager; |
46 | | - private BottomNavigationBar navigationBar; |
47 | | - private final List<Fragment> fragmentList = new ArrayList<>(); |
48 | | - |
49 | | - public BottomBarUtil(AppCompatActivity activity, int rootViewId, int barId) { |
50 | | - this.rootViewId = rootViewId; |
51 | | - |
52 | | - fragmentManager = activity.getSupportFragmentManager(); |
53 | | - navigationBar = (BottomNavigationBar) activity.findViewById(barId); |
54 | | - navigationBar.setMode(BottomNavigationBar.MODE_DEFAULT); |
55 | | - } |
56 | | - |
57 | | - public void addItem(Fragment fragment, String title, Integer imageId, int color) { |
58 | | - fragmentList.add(fragment); |
59 | | - navigationBar.addItem(new BottomNavigationItem(imageId, title).setInActiveColor(color)); |
60 | | - } |
61 | | - |
62 | | - public void initialise() { |
63 | | - FragmentTransaction transaction = fragmentManager.beginTransaction(); |
64 | | - for (Fragment fragment : fragmentList) { |
65 | | - transaction.add(rootViewId, fragment); |
66 | | - } |
67 | | - transaction.commit(); |
68 | | - showFragment(0); |
69 | | - navigationBar.initialise(); |
70 | | - navigationBar.setTabSelectedListener(this); |
| 92 | + /** |
| 93 | + * 初始化容器,添加 fragment |
| 94 | + * |
| 95 | + * @param containerViewId 容器 ID |
| 96 | + */ |
| 97 | + public void initialise(int containerViewId) { |
| 98 | + mBottomNavigationBar.setMode(mMode); |
| 99 | + mBottomNavigationBar.setBackgroundStyle(mBackgroundStyle); |
| 100 | + FragmentTransaction transaction = mFragmentManager.beginTransaction(); |
| 101 | + for (Fragment fragment : fragmentList) { |
| 102 | + transaction.add(containerViewId, fragment); |
71 | 103 | } |
| 104 | + transaction.commit(); |
| 105 | + showFragment(0); |
| 106 | + mBottomNavigationBar.initialise(); |
| 107 | + } |
72 | 108 |
|
73 | | - private void showFragment(int position) { |
74 | | - FragmentTransaction transaction = fragmentManager.beginTransaction(); |
75 | | - for (int i = 0; i < fragmentList.size(); i++) { |
76 | | - if (i != position) { |
77 | | - transaction.hide(fragmentList.get(i)); |
78 | | - } else { |
79 | | - transaction.show(fragmentList.get(i)); |
80 | | - } |
| 109 | + /** |
| 110 | + * 显示 fragment |
| 111 | + * |
| 112 | + * @param position 位置 |
| 113 | + */ |
| 114 | + private void showFragment(int position) { |
| 115 | + FragmentTransaction transaction = mFragmentManager.beginTransaction(); |
| 116 | + for (int i = 0; i < fragmentList.size(); i++) { |
| 117 | + if (i != position) { |
| 118 | + transaction.hide(fragmentList.get(i)); |
| 119 | + } else { |
| 120 | + transaction.show(fragmentList.get(i)); |
81 | 121 | } |
82 | | - transaction.commit(); |
83 | 122 | } |
| 123 | + transaction.commit(); |
| 124 | + } |
84 | 125 |
|
85 | | - @Override |
86 | | - public void onTabSelected(int position) { |
87 | | - navigationBar.selectTab(position); |
88 | | - showFragment(position); |
89 | | - BaseTabBottomActivity.this.onTabSelected(position); |
90 | | - } |
| 126 | + @Override |
| 127 | + public void onTabSelected(int position) { |
| 128 | + showFragment(position); |
| 129 | + } |
91 | 130 |
|
92 | | - @Override |
93 | | - public void onTabUnselected(int position) { |
| 131 | + @Override |
| 132 | + public void onTabUnselected(int position) { |
94 | 133 |
|
95 | | - } |
| 134 | + } |
96 | 135 |
|
97 | | - @Override |
98 | | - public void onTabReselected(int position) { |
| 136 | + @Override |
| 137 | + public void onTabReselected(int position) { |
99 | 138 |
|
100 | | - } |
101 | 139 | } |
102 | 140 | } |
0 commit comments