Android Studio - 第四十八期 模块ViewPager+Fragment
最近一直在review撸撸的代码,发现了一种模块的写法,非常不错,独立出来,希望能帮到你~
如果你遇到这样的页面,怎么办,不会把所有代码都写到一个页面中吧~,这样看你代码的人会骂死你的吧~我想~而且如果不同的版本要用不同的位置,大小也不一样,难道你要重新布局嘛~这都是开发中需要纠结的,下面就开始正题了,这是利用了以前讲过的多版本打版以及配置多Fragment加载巧妙的解决了复杂的页面逻辑,我数了数,首页代码不到一百行,厉害吧~哈哈哈哈~
在写这样的页面之前给大家介绍一下怎么写一个页面模块代码。
demo1:单Activity页面多模块单版本
Demo1FragmentFactory:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo1.factorys;
import
android.support.v4.util.SparseArrayCompat;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo1.fragments.Demo1Fragment1;
import
com.example.p031_mokuaihua_viewpager_fragment.demo1.fragments.Demo1Fragment2;
/**
* Created by shining on 2017/2/27 0027.
*/
public
class
Demo1FragmentFactory {
private
static
SparseArrayCompat<Class<?
extends
BaseFragment>> sIndexFragments =
new
SparseArrayCompat<>();
static
{
sIndexFragments.put(R.id.demo1_page_0_item_0, Demo1Fragment1.
class
);
//模块1
sIndexFragments.put(R.id.demo1_page_0_item_1, Demo1Fragment2.
class
);
//模块2
}
public
static
Class<?
extends
BaseFragment> get(
int
id) {
if
(sIndexFragments.indexOfKey(id) <
0
) {
throw
new
UnsupportedOperationException(
"cannot find fragment by "
+ id);
}
return
sIndexFragments.get(id);
}
public
static
SparseArrayCompat<Class<?
extends
BaseFragment>> get() {
return
sIndexFragments;
}
}
|
ComFragmentHelper:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package
com.example.p031_mokuaihua_viewpager_fragment.utils;
import
android.os.Bundle;
import
android.support.v4.app.Fragment;
/**
* Created by shining on 2016/12/21 0021.
*/
public
class
ComFragmentHelper {
/**
* 新建fragment实例
* @param fragmentKlass
* @param bundle
* @param <T>
* @return
*/
@SuppressWarnings
(
"unchecked"
)
public
static
<T
extends
Fragment> T newFragment(Class<T> fragmentKlass, Bundle bundle) {
T res =
null
;
try
{
res = fragmentKlass.newInstance();
if
(bundle !=
null
) {
res.setArguments(bundle);
}
}
catch
(InstantiationException e) {
e.printStackTrace();
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
return
res;
}
/**
* 根据fragment的完整包名+名称实例化fragment
* @param className
* @param bundle
* @param <T>
* @return
*/
@SuppressWarnings
(
"unchecked"
)
public
static
<T
extends
Fragment> T newFragment(String className, Bundle bundle) {
T res =
null
;
try
{
res = (T) Class.forName(className).newInstance();
if
(bundle !=
null
) {
res.setArguments(bundle);
}
}
catch
(InstantiationException e) {
e.printStackTrace();
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
return
res;
}
}
|
Demo1Fragment1:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo1.fragments;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.view.View;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexNetFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo1.Demo1Activity;
/**
* Created by shining on 2017/8/14.
*/
public
class
Demo1Fragment1
extends
BaseIndexNetFragment {
@Override
public
void
call(Object value) {
}
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo1_fragment1;
}
@Override
protected
void
setup(View rootView,
@Nullable
Bundle savedInstanceState) {
super
.setup(rootView, savedInstanceState);
rootView.findViewById(R.id.tv1).setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
SendToFragment(
"demo1的fragment1页面"
);
}
});
}
/**
* 页面传值操作部分
*
* @param id1
*/
private
void
SendToFragment(String id1) {
//举例
// IndexFoodFragmentUpdateIds iff = new IndexFoodFragmentUpdateIds();
// iff.setFood_definition_id(id1);
// iff.setFood_name(id2);
if
(getActivity() !=
null
&& getActivity()
instanceof
Demo1Activity) {
((Demo1Activity) getActivity()).callFragment(id1, Demo1Fragment2.
class
.getName());
}
}
}
|
Demo1Fragment2:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo1.fragments;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.view.View;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexNetFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.ToastUtil;
/**
* Created by shining on 2017/8/14.
*/
public
class
Demo1Fragment2
extends
BaseIndexNetFragment {
@Override
public
void
call(Object value) {
String ids = (String) value;
ToastUtil.showToastShort(ids);
}
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo1_fragment2;
}
@Override
protected
void
setup(View rootView,
@Nullable
Bundle savedInstanceState) {
super
.setup(rootView, savedInstanceState);
}
}
|
这里注意Fragment之间如果需要通信,可以用下面的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 页面传值操作部分
*
* @param id1
*/
private
void
SendToFragment(String id1) {
//举例
// IndexFoodFragmentUpdateIds iff = new IndexFoodFragmentUpdateIds();
// iff.setFood_definition_id(id1);
// iff.setFood_name(id2);
if
(getActivity() !=
null
&& getActivity()
instanceof
Demo1Activity) {
((Demo1Activity) getActivity()).callFragment(id1, Demo1Fragment2.
class
.getName());
}
}
//接收传值处理逻辑bufen
@Override
public
void
call(Object value) {
String ids = (String) value;
ToastUtil.showToastShort(ids);
}
|
Demo1Activity:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo1;
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.util.SparseArrayCompat;
import
android.text.TextUtils;
import
android.view.View;
import
android.view.View.OnClickListener;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseActivity;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo1.factorys.Demo1FragmentFactory;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.ComFragmentHelper;
public
class
Demo1Activity
extends
BaseActivity
implements
OnClickListener {
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo1;
}
@Override
protected
void
setup(
@Nullable
Bundle savedInstanceState) {
super
.setup(savedInstanceState);
findview();
onclickListener();
doNetWork();
}
private
void
doNetWork() {
}
private
void
onclickListener() {
}
private
void
findview() {
setupFragments();
}
/**
* 初始化首页fragments
*/
private
void
setupFragments() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SparseArrayCompat<Class<?
extends
BaseFragment>> array = Demo1FragmentFactory.get();
//一个版本模式bufen
int
size = array.size();
BaseFragment item;
for
(
int
i =
0
; i < size; i++) {
item = ComFragmentHelper.newFragment(array.valueAt(i),
null
);
ft.replace(array.keyAt(i), item, item.getClass().getName());
}
ft.commitAllowingStateLoss();
}
@Override
public
void
onClick(View v) {
}
/**
* fragment间通讯bufen
*
* @param value 要传递的值
* @param tag 要通知的fragment的tag
*/
public
void
callFragment(Object value, String... tag) {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment;
for
(String item : tag) {
if
(TextUtils.isEmpty(item)) {
continue
;
}
fragment = fm.findFragmentByTag(item);
if
(fragment !=
null
&& fragment
instanceof
BaseIndexFragment) {
((BaseIndexFragment) fragment).call(value);
}
}
}
}
|
效果如下图:
demo2:单Activity页面多模块多版本
Demo2Config1:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo2.configs;
import
android.content.Context;
import
android.content.pm.ApplicationInfo;
import
android.content.pm.PackageManager;
import
android.support.v4.util.SparseArrayCompat;
import
android.text.TextUtils;
import
com.example.p031_mokuaihua_viewpager_fragment.applications.DemoApplication;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.MyLogUtil;
/**
* <p>function: </p>
* <p>description: </p>
* <p>history: 1. 2017/3/23</p>
* <p>Author: geek</p>
* <p>modification:</p>
*/
public
class
Demo2Config1 {
private
static
final
String INDEX_META_DATA =
"DEMO2_CONFIG"
;
/** viewpager页大小*/
// public static int PAGE_COUNT;
/** viewpager每页的itemview id*/
// public static String PAGE_ID;
/** 默认显示第几页*/
// public static int DEFAULT_PAGE_INDEX;
/**
* fragment配置
*/
private
static
SparseArrayCompat<Class<?
extends
BaseFragment>> sIndexFragments =
new
SparseArrayCompat<>();
public
static
void
config() {
Context ctx = DemoApplication.get();
ApplicationInfo info =
null
;
try
{
info = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
}
catch
(PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if
(info ==
null
) {
throw
new
UnsupportedOperationException();
}
String klassName = info.metaData.getString(INDEX_META_DATA);
if
(TextUtils.isEmpty(klassName)) {
throw
new
UnsupportedOperationException(
"please config "
+ INDEX_META_DATA +
" value"
);
}
if
(klassName.startsWith(
"."
)) {
klassName = DemoApplication.get().getPackageName() + klassName;
}
MyLogUtil.d(
"geek"
, klassName);
try
{
Class<?> klass = Class.forName(klassName);
klass.getDeclaredMethod(
"setup"
).invoke(
null
);
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
Class<?
extends
BaseFragment> getFragment(
int
id) {
if
(sIndexFragments.indexOfKey(id) <
0
) {
throw
new
UnsupportedOperationException(
"cannot find fragment by "
+ id);
}
return
sIndexFragments.get(id);
}
public
static
SparseArrayCompat<Class<?
extends
BaseFragment>> getFragments() {
return
sIndexFragments;
}
}
|
Demo2Factory1:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo2.factorys;
import
android.support.v4.util.SparseArrayCompat;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo2.configs.Demo2Config1;
import
com.example.p031_mokuaihua_viewpager_fragment.demo2.fragments.Demo2Fragment1;
import
com.example.p031_mokuaihua_viewpager_fragment.demo2.fragments.Demo2Fragment2;
public
class
Demo2Factory1 {
public
static
void
setup() {
// IndexConfig.PAGE_COUNT = 3;
// IndexConfig.PAGE_ID = "old_pager_index_";
// IndexConfig.DEFAULT_PAGE_INDEX = 1;
registerFragments(Demo2Config1.getFragments());
}
private
static
void
registerFragments(SparseArrayCompat<Class<?
extends
BaseFragment>> sIndexFragments) {
sIndexFragments.put(R.id.demo2_page_0_item_1, Demo2Fragment1.
class
);
//菜谱
sIndexFragments.put(R.id.demo2_page_0_item_2, Demo2Fragment2.
class
);
//视频
}
}
|
build.gradle:(多版本打版)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
def currentMode = flavor.versionName.split(
"_"
)[
3
]
def currentEnvironment = flavor.versionName.split(
"_"
)[
1
]
def stValue =
true
// t == currentEnvironment 以前的判断条件
if
(currentEnvironment.endsWith(
"T"
)) {
//判断是否为测试版 是否以T结尾
stValue =
false
}
else
{
stValue =
true
}
if
(currentMode == demo1) {
flavor.manifestPlaceholders = [DEMO2_CONFIG_VALUE:
".demo2.factorys.Demo2Factory1"
, STATISTICS_VALUE: stValue]
}
else
if
(currentMode == demo2) {
flavor.manifestPlaceholders = [DEMO2_CONFIG_VALUE:
".demo2.factorys.Demo2Factory2"
, STATISTICS_VALUE: stValue]
}
|
Demo2Activity:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo2;
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.util.SparseArrayCompat;
import
android.text.TextUtils;
import
android.view.View;
import
android.view.View.OnClickListener;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.applications.ConstantNetUtil;
import
com.example.p031_mokuaihua_viewpager_fragment.applications.NetConfig;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseActivity;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo2.configs.Demo2Config1;
import
com.example.p031_mokuaihua_viewpager_fragment.demo2.configs.Demo2Config2;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.ComFragmentHelper;
public
class
Demo2Activity
extends
BaseActivity
implements
OnClickListener{
@Override
protected
void
onCreate(Bundle savedInstanceState) {
if
(ConstantNetUtil.VERSION_APK == NetConfig.version_name1) {
Demo2Config1.config();
}
else
if
(ConstantNetUtil.VERSION_APK == NetConfig.version_name2) {
Demo2Config2.config();
}
super
.onCreate(savedInstanceState);
setupFragments();
findview();
onclickListener();
doNetWork();
}
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo2;
}
@Override
protected
void
setup(
@Nullable
Bundle savedInstanceState) {
super
.setup(savedInstanceState);
// setupFragments();
// findview();
// onclickListener();
// doNetWork();
}
/**
* 初始化首页fragments
*/
private
void
setupFragments() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//TODO 多版本模式bufen
SparseArrayCompat<Class<?
extends
BaseFragment>> array = which_version_fragment_config();
//
int
size = array.size();
BaseFragment item;
for
(
int
i =
0
; i < size; i++) {
item = ComFragmentHelper.newFragment(array.valueAt(i),
null
);
ft.replace(array.keyAt(i), item, item.getClass().getName());
}
ft.commitAllowingStateLoss();
}
private
SparseArrayCompat<Class<?
extends
BaseFragment>> which_version_fragment_config() {
if
(ConstantNetUtil.VERSION_APK == NetConfig.version_name1) {
return
Demo2Config1.getFragments();
}
else
if
(ConstantNetUtil.VERSION_APK == NetConfig.version_name2) {
return
Demo2Config2.getFragments();
}
return
Demo2Config1.getFragments();
}
private
void
doNetWork() {
}
private
void
onclickListener() {
}
private
void
findview() {
}
@Override
public
void
onClick(View v) {
}
/**
* fragment间通讯bufen
*
* @param value 要传递的值
* @param tag 要通知的fragment的tag
*/
public
void
callFragment(Object value, String... tag) {
FragmentManager fm = getSupportFragmentManager();
Fragment fragment;
for
(String item : tag) {
if
(TextUtils.isEmpty(item)) {
continue
;
}
fragment = fm.findFragmentByTag(item);
if
(fragment !=
null
&& fragment
instanceof
BaseIndexFragment) {
((BaseIndexFragment) fragment).call(value);
}
}
}
}
|
效果如下图:
下面就是最复杂的一种需求:demo3 单Activity页面多模块单版本两个Viewpager
Demo3Config:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo3.configs;
import
android.content.Context;
import
android.content.pm.ApplicationInfo;
import
android.content.pm.PackageManager;
import
android.support.v4.util.SparseArrayCompat;
import
android.text.TextUtils;
import
com.example.p031_mokuaihua_viewpager_fragment.applications.DemoApplication;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.MyLogUtil;
public
class
Demo3Config {
private
static
final
String INDEX_META_DATA =
"DEMO3_CONFIG"
;
/** viewpager页大小*/
public
static
int
PAGE_COUNT;
/** viewpager每页的itemview id*/
public
static
String PAGE_LAYOUT_ID;
/** 默认显示第几页*/
public
static
int
DEFAULT_PAGE_INDEX;
/**
* fragment配置
*/
private
static
SparseArrayCompat<Class<?
extends
BaseFragment>> sIndexFragments =
new
SparseArrayCompat<>();
public
static
void
config() {
Context ctx = DemoApplication.get();
ApplicationInfo info =
null
;
try
{
info = ctx.getPackageManager().getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
}
catch
(PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if
(info ==
null
) {
throw
new
UnsupportedOperationException();
}
String klassName = info.metaData.getString(INDEX_META_DATA);
if
(TextUtils.isEmpty(klassName)) {
throw
new
UnsupportedOperationException(
"please config "
+ INDEX_META_DATA +
" value"
);
}
if
(klassName.startsWith(
"."
)) {
klassName = DemoApplication.get().getPackageName() + klassName;
}
MyLogUtil.d(
"geek"
, klassName);
try
{
Class<?> klass = Class.forName(klassName);
klass.getDeclaredMethod(
"setup"
).invoke(
null
);
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
Class<?
extends
BaseFragment> getFragment(
int
id) {
if
(sIndexFragments.indexOfKey(id) <
0
) {
throw
new
UnsupportedOperationException(
"cannot find fragment by "
+ id);
}
return
sIndexFragments.get(id);
}
public
static
SparseArrayCompat<Class<?
extends
BaseFragment>> getFragments() {
return
sIndexFragments;
}
}
|
Demo3Factory:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo3.factorys;
import
android.support.v4.util.SparseArrayCompat;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.configs.Demo3Config;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments.Demo3Fragment10;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments.Demo3Fragment11;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments.Demo3Fragment20;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments.Demo3Fragment21;
/**
* 首页模块fragment的工厂, 首页模块有需要更换的,可以在此修改,格式为id->Fragment.class<br />
* Created by shining on 2016/8/1.
*/
public
class
Demo3Factory {
public
static
void
setup() {
Demo3Config.PAGE_COUNT =
2
;
Demo3Config.PAGE_LAYOUT_ID =
"activity_demo3_layout_pager_item_"
;
Demo3Config.DEFAULT_PAGE_INDEX =
0
;
registerFragments(Demo3Config.getFragments());
}
private
static
void
registerFragments(SparseArrayCompat<Class<?
extends
BaseFragment>> sIndexFragments) {
sIndexFragments.put(R.id.fragment_demo3_pager_index_0_0, Demo3Fragment10.
class
);
//第一屏 layout1
sIndexFragments.put(R.id.fragment_demo3_pager_index_0_1, Demo3Fragment11.
class
);
//第一屏 layout2
sIndexFragments.put(R.id.fragment_demo3_pager_index_1_0, Demo3Fragment20.
class
);
//第二屏 layout1
sIndexFragments.put(R.id.fragment_demo3_pager_index_1_1, Demo3Fragment21.
class
);
//第二屏 layout2
}
}
|
Demo3Fragment10:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments;
import
android.content.Context;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.view.View;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexNetFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.demo3.Demo3Activity;
/**
* Created by shining on 2017/8/14.
*/
public
class
Demo3Fragment10
extends
BaseIndexNetFragment {
private
Context mContext;
@Override
public
void
onCreate(
@Nullable
Bundle bundle) {
super
.onCreate(bundle);
mContext = getActivity();
}
@Override
public
void
call(Object value) {
}
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo3_fragment10;
}
@Override
protected
void
setup(View rootView,
@Nullable
Bundle savedInstanceState) {
super
.setup(rootView, savedInstanceState);
rootView.findViewById(R.id.tv1).setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
SendToFragment(
"demo3的fragment1页面"
);
((Demo3Activity) mContext).changeView(
1
);
}
});
}
/**
* 页面传值操作部分
*
* @param id1
*/
private
void
SendToFragment(String id1) {
//举例
// IndexFoodFragmentUpdateIds iff = new IndexFoodFragmentUpdateIds();
// iff.setFood_definition_id(id1);
// iff.setFood_name(id2);
if
(getActivity() !=
null
&& getActivity()
instanceof
Demo3Activity) {
((Demo3Activity) getActivity()).callFragment(id1, Demo3Fragment20.
class
.getName());
}
}
}
|
Demo3Fragment20:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo3.fragments;
import
android.os.Bundle;
import
android.support.annotation.Nullable;
import
android.view.View;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
import
com.example.p031_mokuaihua_viewpager_fragment.base.BaseIndexNetFragment;
import
com.example.p031_mokuaihua_viewpager_fragment.utils.ToastUtil;
/**
* Created by shining on 2017/8/14.
*/
public
class
Demo3Fragment20
extends
BaseIndexNetFragment {
@Override
public
void
call(Object value) {
String ids = (String) value;
ToastUtil.showToastShort(ids);
}
@Override
protected
int
getLayoutId() {
return
R.layout.activity_demo3_fragment20;
}
@Override
protected
void
setup(View rootView,
@Nullable
Bundle savedInstanceState) {
super
.setup(rootView, savedInstanceState);
}
}
|
这里的导航点也可以自定义:给大家提供一个util 有圆的 方的 长方形 水滴
IndexPagerIndicator:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package
com.example.p031_mokuaihua_viewpager_fragment.demo3.utils;
import
android.content.Context;
import
android.content.res.TypedArray;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Rect;
import
android.os.Bundle;
import
android.os.Parcelable;
import
android.support.v4.view.ViewPager;
import
android.text.TextUtils;
import
android.util.AttributeSet;
import
android.view.View;
import
android.widget.LinearLayout;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
/**
* 首页viewpager指示符<br/>
* Created by geek on 2016/7/29.
*/
public
class
IndexPagerIndicator
extends
LinearLayout {
private
int
mItemWidth;
private
int
mItemSelectedHeight;
private
int
mItemInterval;
private
int
mItemColor;
private
int
mStartX;
private
int
mCurrentX;
private
Paint mSelectedPaint;
private
IStyleDrawer mStyleDrawer;
public
IndexPagerIndicator(Context context) {
this
(context,
null
,
0
);
}
public
IndexPagerIndicator(Context context, AttributeSet attrs) {
this
(context, attrs,
0
);
}
public
IndexPagerIndicator(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
setBackgroundColor(Color.TRANSPARENT);
setOrientation(LinearLayout.HORIZONTAL);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IndexPagerIndicator, defStyleAttr,
0
);
mItemWidth = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_width,
10
);
int
itemHeight = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_height,
2
);
mItemSelectedHeight = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_select_height,
0
);
if
(mItemSelectedHeight ==
0
) { mItemSelectedHeight = itemHeight;}
mItemInterval = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_interval,
0
);
mItemColor = ta.getColor(R.styleable.IndexPagerIndicator_indicator_item_color, Color.BLACK);
int
selectedColor = ta.getColor(R.styleable.IndexPagerIndicator_indicator_select_color, Color.WHITE);
String styleDrawer = ta.getString(R.styleable.IndexPagerIndicator_indicator_style_drawer);
try
{
if
(TextUtils.isEmpty(styleDrawer)) {
throw
new
Exception();
}
mStyleDrawer = (IStyleDrawer) Class.forName(styleDrawer).newInstance();
}
catch
(Exception e) {
e.printStackTrace();
mStyleDrawer =
new
DefaultStyleDrawer();
}
ta.recycle();
mStyleDrawer.prepare(mItemWidth, itemHeight, mItemSelectedHeight);
mSelectedPaint =
new
Paint(Paint.ANTI_ALIAS_FLAG);
mSelectedPaint.setColor(selectedColor);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
if
(mItemSelectedHeight > getMeasuredHeight()) {
setMeasuredDimension(getMeasuredWidth(), mItemSelectedHeight);
}
mStartX = (getMeasuredWidth() - (getChildCount() * mItemWidth
|




