Skip to content

Commit 2c22394

Browse files
committed
1:调整目录结构,增加Fragment组件化示例;
1 parent 5caabd1 commit 2c22394

File tree

14 files changed

+222
-24
lines changed

14 files changed

+222
-24
lines changed

module_main/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ android {
2020
arguments = [moduleName: project.getName()]
2121
}
2222
}
23+
vectorDrawables.useSupportLibrary = true
2324
}
2425

2526

@@ -37,10 +38,13 @@ android {
3738
}
3839
}
3940

41+
4042
}
4143

4244
dependencies {
4345
compile fileTree(dir: 'libs', include: ['*.jar'])
4446
annotationProcessor "com.alibaba:arouter-compiler:$rootProject.annotationProcessor"
4547
compile project(':lib_common')
48+
compile 'com.android.support:design:25.3.1'
49+
compile 'com.android.support:support-vector-drawable:25.3.1'
4650
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.guiying.main">
3+
package="com.guiying.module.main">
34

45
<application android:theme="@style/AppTheme">
5-
<activity android:name=".MainActivity">
6+
<activity android:name="com.guiying.module.main.MainActivity">
67
<intent-filter>
78
<action android:name="android.intent.action.MAIN" />
89

910
<category android:name="android.intent.category.LAUNCHER" />
1011
</intent-filter>
1112
</activity>
13+
<activity
14+
android:name=".BottomNavigationActivity"
15+
android:label="@string/title_activity_bottom_navigation">
16+
17+
</activity>
1218
</application>
1319

14-
</manifest>
20+
</manifest>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.guiying.module.main;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import android.support.design.widget.BottomNavigationView;
7+
import android.view.MenuItem;
8+
9+
import com.guiying.module.common.base.BaseActivity;
10+
import com.guiying.module.common.base.BaseFragment;
11+
import com.guiying.module.common.base.ViewManager;
12+
import com.guiying.module.common.widget.NoScrollViewPager;
13+
14+
import java.util.List;
15+
16+
/**
17+
* <p> </p>
18+
*
19+
* @author 张华洋 2017/9/27 10:23
20+
* @version V1.1
21+
* @name BottomNavigationActivity
22+
*/
23+
public class BottomNavigationActivity extends BaseActivity {
24+
25+
private NoScrollViewPager mPager;
26+
private List<BaseFragment> mFragments;
27+
private FragmentAdapter mAdapter;
28+
29+
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
30+
= new BottomNavigationView.OnNavigationItemSelectedListener() {
31+
32+
@Override
33+
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
34+
int i = item.getItemId();
35+
if (i == R.id.navigation_home) {
36+
mPager.setCurrentItem(0);
37+
return true;
38+
} else if (i == R.id.navigation_dashboard) {
39+
mPager.setCurrentItem(1);
40+
return true;
41+
} else if (i == R.id.navigation_notifications) {
42+
mPager.setCurrentItem(2);
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
};
49+
50+
@Override
51+
protected void onCreate(@Nullable Bundle savedInstanceState) {
52+
super.onCreate(savedInstanceState);
53+
setContentView(R.layout.activity_bottom_navigation);
54+
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
55+
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
56+
initViewPager();
57+
}
58+
59+
private void initViewPager() {
60+
mFragments = ViewManager.getInstance().getAllFragment();
61+
mPager = (NoScrollViewPager) findViewById(R.id.container_pager);
62+
mAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments);
63+
mPager.setPagerEnabled(false);
64+
mPager.setAdapter(mAdapter);
65+
}
66+
67+
68+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.guiying.module.main;
2+
3+
import android.support.v4.app.Fragment;
4+
import android.support.v4.app.FragmentManager;
5+
import android.support.v4.app.FragmentStatePagerAdapter;
6+
7+
import com.guiying.module.common.base.BaseFragment;
8+
9+
import java.util.List;
10+
11+
/**
12+
* <p>Fragments适配器 </p>
13+
*
14+
* @author 张华洋 2017/9/27 10:14
15+
* @version V1.1
16+
* @name ResourcePagerAdapter
17+
*/
18+
public class FragmentAdapter extends FragmentStatePagerAdapter {
19+
private List<BaseFragment> mFragments;
20+
21+
public FragmentAdapter(FragmentManager fm, List<BaseFragment> mFragments) {
22+
super(fm);
23+
this.mFragments = mFragments;
24+
}
25+
26+
@Override
27+
public Fragment getItem(int position) {
28+
return mFragments.get(position);
29+
}
30+
31+
@Override
32+
public int getCount() {
33+
return mFragments != null ? mFragments.size() : 0;
34+
}
35+
36+
@Override
37+
public int getItemPosition(Object object) {
38+
return android.support.v4.view.PagerAdapter.POSITION_NONE;
39+
}
40+
}

module_main/src/main/java/com/guiying/main/MainActivity.java renamed to module_main/src/main/java/com/guiying/module/main/MainActivity.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.guiying.main;
1+
package com.guiying.module.main;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.view.KeyEvent;
56
import android.view.View;
6-
import android.widget.Button;
77

88
import com.alibaba.android.arouter.launcher.ARouter;
9-
import com.guiying.common.base.BaseActivity;
10-
import com.guiying.common.base.BaseApplication;
11-
import com.guiying.common.utils.ToastUtils;
9+
import com.guiying.module.common.base.BaseActivity;
10+
import com.guiying.module.common.base.ViewManager;
11+
import com.guiying.module.common.utils.ToastUtils;
1212

1313
/**
1414
* <p>类说明</p>
@@ -19,18 +19,15 @@
1919
*/
2020
public class MainActivity extends BaseActivity implements View.OnClickListener {
2121

22-
private long exitTime = 0;
23-
protected Button newsButton;
24-
protected Button girlsButton;
22+
private long mExitTime = 0;
2523

2624
@Override
2725
protected void onCreate(Bundle savedInstanceState) {
2826
super.onCreate(savedInstanceState);
2927
setContentView(R.layout.activity_main);
30-
newsButton = (Button) findViewById(R.id.news_button);
31-
newsButton.setOnClickListener(MainActivity.this);
32-
girlsButton = (Button) findViewById(R.id.girls_button);
33-
girlsButton.setOnClickListener(MainActivity.this);
28+
findViewById(R.id.news_button).setOnClickListener(this);
29+
findViewById(R.id.girls_button).setOnClickListener(this);
30+
findViewById(R.id.fragment_button).setOnClickListener(this);
3431
}
3532

3633
@Override
@@ -41,6 +38,8 @@ public void onClick(View view) {
4138
} else if (view.getId() == R.id.girls_button) {
4239
//跳转到GirlsActivity
4340
ARouter.getInstance().build("/girls/list").navigation();
41+
} else if (view.getId() == R.id.fragment_button) {
42+
startActivity(new Intent(this, BottomNavigationActivity.class));
4443
}
4544
}
4645

@@ -49,11 +48,11 @@ public void onClick(View view) {
4948
public boolean onKeyDown(int keyCode, KeyEvent event) {
5049
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
5150
//两秒之内按返回键就会退出
52-
if ((System.currentTimeMillis() - exitTime) > 2000) {
51+
if ((System.currentTimeMillis() - mExitTime) > 2000) {
5352
ToastUtils.showShortToast(getString(R.string.app_exit_hint));
54-
exitTime = System.currentTimeMillis();
53+
mExitTime = System.currentTimeMillis();
5554
} else {
56-
BaseApplication.getIns().exitApp(this);
55+
ViewManager.getInstance().exitApp(this);
5756
}
5857
return true;
5958
}

module_main/src/main/java/debug/MainApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package debug;
22

3-
import com.guiying.common.base.BaseApplication;
4-
import com.guiying.common.http.HttpClient;
5-
import com.guiying.common.http.OnResultListener;
3+
import com.guiying.module.common.base.BaseApplication;
4+
import com.guiying.module.common.http.HttpClient;
5+
import com.guiying.module.common.http.OnResultListener;
66
import com.orhanobut.logger.Logger;
77

88
/**

module_main/src/main/module/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.guiying.main">
3+
package="com.guiying.module">
44

55
<application
66
android:name="debug.MainApplication"
@@ -9,7 +9,7 @@
99
android:label="@string/app_main"
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".main.MainActivity">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M3,13h8L11,3L3,3v10zM3,21h8v-6L3,15v6zM13,21h8L21,11h-8v10zM13,3v6h8L21,3h-8z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="24.0"
5+
android:viewportWidth="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z" />
9+
</vector>

0 commit comments

Comments
 (0)