请先看下面的class图示:
来解释一下图中的意思:
这里的FragmentCallBack 是一个接口定义了Fragment 和Activity之间通信时候调用的方法。FragmentLearningActivity本身 实现了这个callback接口,从泛型角度,它就是这个接口的具体实现类。
而在TopFragment中定义了一个FragmentCallBack的实例callBack,所以当初始化的TopFragment的时候,就把Fragment所属的Activity也就是FragmentLearningActivity注册给这个实例callBack。
当TopFragment有事件触发的时候,就调用这个接口实例callBack调用invokeItemSelect方法,因为callBack就是泛指FragmentLearningActivity,所以FragmentLearningActivity中的invokeItemSelect就会触发,这通过这个方法就可以实现具体的通信。
其实这种方式在popupwindow和Activity通信方式也会被用到。所以大家要经常思考加以扩展和复用。
下面是一个Fragment和Activity之间通信的例子:
TopFragment实现:
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
|
package
com
.
learning
.
coolbeetle
.
fragment
;
import
android
.
R
;
import
android
.
annotation
.
SuppressLint
;
import
android
.
app
.
Activity
;
import
android
.
app
.
Fragment
;
import
android
.
os
.
Bundle
;
import
android
.
view
.
LayoutInflater
;
import
android
.
view
.
View
;
import
android
.
view
.
ViewGroup
;
import
android
.
widget
.
AdapterView
;
import
android
.
widget
.
AdapterView
.
OnItemClickListener
;
import
android
.
widget
.
ArrayAdapter
;
import
android
.
widget
.
ListView
;
import
com
.
learning
.
coolbeetle
.
model
.
BookContent
;
@SuppressLint
(
"NewApi"
)
public
class
TopFragment
extends
Fragment
implements
OnItemClickListener
{
ListView
listview
;
FragmentCallBack
callBack
;
@Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
}
@Override
public
View
onCreateView
(
LayoutInflater
inflater
,
ViewGroup
container
,
Bundle
savedInstanceState
)
{
View
view
=
inflater
.
inflate
(
com
.
learning
.
coolbeetle
.
R
.
layout
.
fragment_top
,
null
)
;
this
.
listview
=
(
ListView
)
view
.
findViewById
(
com
.
learning
.
coolbeetle
.
R
.
id
.
top_list
)
;
this
.
listview
.
setOnItemClickListener
(
this
)
;
listview
.
setAdapter
(
new
ArrayAdapter
(
this
.
getActivity
(
)
,
R
.
layout
.
simple_list_item_activated_1
,
R
.
id
.
text1
,
BookContent
.
ITEMS
)
)
;
return
view
;
}
@Override
public
void
onAttach
(
Activity
activity
)
{
super
.
onAttach
(
activity
)
;
if
(
!
(
activity
instanceof
FragmentCallBack
)
)
{
throw
new
IllegalStateException
(
"Activity must implement FragmentCallBack interface!"
)
;
}
this
.
callBack
=
(
FragmentCallBack
)
activity
;
}
@Override
public
void
onDetach
(
)
{
super
.
onDetach
(
)
;
callBack
=
null
;
}
@Override
public
void
onItemClick
(
AdapterView
<
?
>
parent
,
View
view
,
int
position
,
long
id
)
{
callBack
.
invokeItemSelect
(
BookContent
.
ITEMS
.
get
(
position
)
.
getId
(
)
)
;
}
public
FragmentCallBack
getCallBack
(
)
{
return
callBack
;
}
public
void
setCallBack
(
FragmentCallBack
callBack
)
{
this
.
callBack
=
callBack
;
}
}
|
FragmentCallBack接口
1
2
3
4
|
package
com
.
learning
.
coolbeetle
.
fragment
;
public
interface
FragmentCallBack
{
public
void
invokeItemSelect
(
Integer
id
)
;
}
|
FragmentLearningActivity的代码实现:
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
|
package
com
.
learning
.
coolbeetle
;
import
android
.
annotation
.
SuppressLint
;
import
android
.
app
.
Activity
;
import
android
.
os
.
Bundle
;
import
com
.
learning
.
coolbeetle
.
fragment
.
DetailFragment
;
import
com
.
learning
.
coolbeetle
.
fragment
.
FragmentCallBack
;
import
com
.
learning
.
coolbeetle
.
model
.
Book
;
import
com
.
learning
.
coolbeetle
.
model
.
BookContent
;
public
class
FragmentLearningActivity
extends
Activity
implements
FragmentCallBack
{
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
fragment_learning
)
;
}
@SuppressLint
(
"NewApi"
)
@Override
public
void
invokeItemSelect
(
Integer
id
)
{
Book
selectedBook
=
BookContent
.
MAP_ITEMS
.
get
(
id
)
;
if
(
selectedBook
!=
null
)
{
Bundle
bundle
=
new
Bundle
(
)
;
bundle
.
putInt
(
"bookId"
,
id
)
;
DetailFragment
detailFragment
=
new
DetailFragment
(
)
;
detailFragment
.
setArguments
(
bundle
)
;
this
.
getFragmentManager
(
)
.
beginTransaction
(
)
.
replace
(
R
.
id
.
detail_fragment
,
detailFragment
)
.
commit
(
)
;
}
}
}
|
Activity布局文件中添加fragment:
1
2
3
4
5
6
7
8
9
10
11
12
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns
:
android
=
"http://schemas.android.com/apk/res/android"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"fill_parent"
android
:
orientation
=
"vertical"
>
<
fragment
android
:
id
=
"@+id/top_fragment"
android
:
layout_width
=
"wrap_content"
android
:
layout_height
=
"wrap_content"
class
=
"com.learning.coolbeetle.fragment.TopFragment"
/
>
<
/
RelativeLayout
>
|