注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛(ài)安卓而產(chǎn)生了翻譯的念頭,純屬個(gè)人興趣愛(ài)好。
原文鏈接:
http://developer.android.com/training/basics/fragments/communicating.html
為了重用Fragment UI組件,你應(yīng)該將每一個(gè)組件構(gòu)建為自控地,模塊化的組件,它們有自己的布局和行為。一旦你定義好了這些可重用的fragment,你就可以將他們與activity關(guān)聯(lián),通過(guò)應(yīng)用邏輯將它們連接起來(lái),以此實(shí)現(xiàn)一個(gè)整體復(fù)合的界面。
你經(jīng)常會(huì)希望一個(gè)fragment可以和另一個(gè)通信,比如想要交換基于用戶(hù)事件的內(nèi)容。所有的“fragment到fragment”的通信通過(guò)他們所關(guān)聯(lián)的activity來(lái)完成。兩個(gè)fragment永遠(yuǎn)無(wú)法直接通信。
?
一). 定義一個(gè)接口
為了允許fragment向上與它隸屬的Activity通信,你可以在這個(gè)fragment中定義一個(gè)接口,然后再activity中實(shí)現(xiàn)。這個(gè)Fragment會(huì)在它的生命周期函數(shù) onAttach()中 捕捉到接口的實(shí)現(xiàn),而后就能調(diào)用接口方法來(lái)與activity通信。
下面是一個(gè)Fragment到Activity通信的例子:
public
class
HeadlinesFragment
extends
ListFragment {
OnHeadlineSelectedListener mCallback;
//
Container Activity must implement this interface
public
interface
OnHeadlineSelectedListener {
public
void
onArticleSelected(
int
position);
}
@Override
public
void
onAttach(Activity activity) {
super
.onAttach(activity);
//
This makes sure that the container activity has implemented
//
the callback interface. If not, it throws an exception
try
{
mCallback
=
(OnHeadlineSelectedListener) activity;
}
catch
(ClassCastException e) {
throw
new
ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener"
);
}
}
...
}
現(xiàn)在,fragment可以通過(guò)調(diào)用 onArticleSelected()方法(或其他接口中的方法)向 activity發(fā)送消息了。在此例中,使用的是 OnHeadlineSelectedListener接口的實(shí)例: mCallback。
比如:下面所示的fragment方法會(huì)在當(dāng)用戶(hù)點(diǎn)擊了一個(gè)列表項(xiàng)時(shí)別調(diào)用。fragment使用回調(diào)接口將事件發(fā)送給父activity。
@Override
public
void
onListItemClick(ListView l, View v,
int
position,
long
id) {
//
Send the event to the host activity
mCallback.onArticleSelected(position);
}
?
二). 實(shí)現(xiàn)接口
為了接收來(lái)自fragment的事件回調(diào),宿主activity必須實(shí)現(xiàn)在fragment類(lèi)中所定義的接口。
下例中的activity實(shí)現(xiàn)了上例中的接口:
public
static
class
MainActivity
extends
Activity
implements
HeadlinesFragment.OnHeadlineSelectedListener{
...
public
void
onArticleSelected(
int
position) {
//
The user selected the headline of an article from the HeadlinesFragment
//
Do something here to display that article
}
}
?
三). 將消息發(fā)送給Fragment
宿主Activity可以通過(guò) findFragmentById() 來(lái)獲取fragment實(shí)例, 之后就能調(diào)用fragment的公有方法, 借此把消息傳遞給fragment。
舉一個(gè)例子:我們想象上述的Activity可能還容納了另一個(gè)fragment,它用來(lái)展示上述回調(diào)方法中歲返回特定項(xiàng)的數(shù)據(jù)。在這個(gè)例子中,activity可以把在回調(diào)方法中收到的信息發(fā)送給另一個(gè)fragment,這個(gè)fragment來(lái)顯示這些數(shù)據(jù)。
public
static
class
MainActivity
extends
Activity
implements
HeadlinesFragment.OnHeadlineSelectedListener{
...
public
void
onArticleSelected(
int
position) {
//
The user selected the headline of an article from the HeadlinesFragment
//
Do something here to display that article
ArticleFragment articleFrag
=
(ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if
(articleFrag !=
null
) {
//
If article frag is available, we're in two-pane layout...
//
Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
}
else
{
//
Otherwise, we're in the one-pane layout and must swap frags...
//
Create fragment and give it an argument for the selected article
ArticleFragment newFragment =
new
ArticleFragment();
Bundle args
=
new
Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction
=
getSupportFragmentManager().beginTransaction();
//
Replace whatever is in the fragment_container view with this fragment,
//
and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(
null
);
//
Commit the transaction
transaction.commit();
}
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

