Skip to content

Commit 9993e02

Browse files
author
lishide
committed
重构底部 Tab 的基础 Activity,增加更多设置 Tab 风格的方法
1 parent c06de2d commit 9993e02

File tree

11 files changed

+140
-110
lines changed

11 files changed

+140
-110
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ allprojects {
1717

1818
```java
1919
dependencies {
20-
compile 'com.github.lishide:BaseAndroidDev:v1.0.0'
20+
compile 'com.github.lishide:BaseAndroidDev:v1.0.3'
2121
}
2222
```
2323

@@ -101,7 +101,7 @@ NoHttp —— 一个有情怀的网络框架 ,让你的网络请求更简单
101101
|compile 'com.mcxiaoke.volley:library:1.0.19'|Volley--网络请求框架|
102102
|compile 'com.google.code.gson:gson:2.7'|Gson--比较常用的 JSON 解析序列化库|
103103
|compile 'com.github.bumptech.glide:glide:3.7.0'|图片加载库 Glide|
104-
|compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'|BottomNavigation--Bottom Navigation Bar|
104+
|compile 'com.ashokvarma.android:bottom-navigation-bar:1.4.1'|BottomNavigation--Bottom Navigation Bar|
105105
|compile 'com.bigkoo:svprogresshud:1.0.6'|SVProgressHUD For Android 精仿 iOS 的提示库|
106106
|compile 'com.yanzhenjie:recyclerview-swipe:1.0.3'|SwipeRecyclerView--RecyclerView 侧滑菜单,长按拖拽,滑动删除,加载更多下拉刷新等|
107107
|compile 'liji.library.dev:citypickerview:0.9.0'|省市区三级联动|

app/src/main/java/com/example/dev/activity/MainActivity.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Bundle;
44

5+
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
56
import com.base.adev.activity.BaseTabBottomActivity;
67
import com.example.dev.R;
78
import com.example.dev.fragment.DemoFragment;
@@ -11,17 +12,18 @@
1112
public class MainActivity extends BaseTabBottomActivity {
1213

1314
@Override
14-
protected void initView() {
15-
addFragment(new HomeFragment(), R.string.tab1, R.mipmap.ic_launcher);
16-
addFragment(new DemoFragment(), R.string.tab2, R.mipmap.ic_launcher);
17-
addFragment(new MeFragment(), R.string.tab_me, R.mipmap.ic_launcher);
18-
19-
initialise();
15+
protected void initContentView(Bundle bundle) {
16+
setContentView(R.layout.activity_base_tab_bottom);
2017
}
2118

2219
@Override
2320
protected void initLogic() {
21+
addItem(new HomeFragment(), R.drawable.ic_tab_home_white_24dp, R.string.tab1, R.color.colorPrimary);
22+
addItem(new DemoFragment(), R.drawable.ic_tab_book_white_24dp, R.string.tab2, R.color.colorPrimaryDark);
23+
addItem(new MeFragment(), R.drawable.ic_tab_tv_white_24dp, R.string.tab_me, R.color.colorAccent);
2424

25+
setNavBarStyle(BottomNavigationBar.MODE_FIXED, BottomNavigationBar.BACKGROUND_STYLE_STATIC);
26+
initialise(R.id.ll_content);
2527
}
2628

2729
@Override
@@ -30,7 +32,7 @@ protected void getBundleExtras(Bundle extras) {
3032
}
3133

3234
@Override
33-
protected void onTabSelected(int position) {
35+
public void onTabSelected(int position) {
3436
super.onTabSelected(position);
3537
}
3638

180 Bytes
Loading
222 Bytes
Loading
189 Bytes
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.design.widget.CoordinatorLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<LinearLayout
9+
android:id="@+id/ll_content"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
12+
android:orientation="vertical"/>
13+
14+
<com.ashokvarma.bottomnavigation.BottomNavigationBar
15+
android:id="@+id/bottom_navigation_bar"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:layout_gravity="bottom"
19+
app:bnbAutoHideEnabled="true"/>
20+
<!--如果不想隐藏BottomNavigationBar,设置 app:bnbAutoHideEnabled="true"-->
21+
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 14 deletions
This file was deleted.

base-android-dev/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
exclude group: 'com.android.support', module: 'support-annotations'
3030
})
3131
compile 'com.android.support:appcompat-v7:25.3.0'
32+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
3233
testCompile 'junit:junit:4.12'
3334
compile 'com.android.support:support-v4:25.3.0'
3435
compile 'com.android.support:design:25.3.0'
@@ -37,7 +38,7 @@ dependencies {
3738

3839
compile 'com.google.code.gson:gson:2.7'
3940
//BottomNavigation
40-
compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'
41+
compile 'com.ashokvarma.android:bottom-navigation-bar:1.4.1'
4142
//图片加载库 Glide
4243
compile 'com.github.bumptech.glide:glide:3.7.0'
4344
//省市区三级联动
Lines changed: 104 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.base.adev.activity;
22

3-
import android.os.Bundle;
43
import android.support.v4.app.Fragment;
54
import android.support.v4.app.FragmentManager;
65
import android.support.v4.app.FragmentTransaction;
7-
import android.support.v7.app.AppCompatActivity;
86

97
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
108
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
@@ -13,90 +11,130 @@
1311
import java.util.ArrayList;
1412
import java.util.List;
1513

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;
1932

2033
@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);
2438
}
2539

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));
2852
}
2953

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));
3266
}
3367

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));
3679
}
3780

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;
4090
}
4191

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);
71103
}
104+
transaction.commit();
105+
showFragment(0);
106+
mBottomNavigationBar.initialise();
107+
}
72108

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));
81121
}
82-
transaction.commit();
83122
}
123+
transaction.commit();
124+
}
84125

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+
}
91130

92-
@Override
93-
public void onTabUnselected(int position) {
131+
@Override
132+
public void onTabUnselected(int position) {
94133

95-
}
134+
}
96135

97-
@Override
98-
public void onTabReselected(int position) {
136+
@Override
137+
public void onTabReselected(int position) {
99138

100-
}
101139
}
102140
}

base-android-dev/src/main/res/layout/base_activity_tab_bottom.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)