亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android Fragment開發(fā)文檔翻譯 - 2

系統(tǒng) 1867 0

由于本人英文能力實在有限,不足之初敬請諒解

本博客只要沒有注明“轉(zhuǎn)”,那么均為原創(chuàng),轉(zhuǎn)貼請注明鏈接

?

android Fragment開發(fā)文檔翻譯 - 1

android Fragment開發(fā)文檔翻譯 - 2

本系列并沒有對原文100%翻譯,也沒有100%的貼出原文

?

與Activity通信

盡管Fragment已經(jīng)作為一個依賴Activity的object實現(xiàn),并且可以在多個activitiy內(nèi)部使用,一個已知的fragment實例是直接與包含它的activity綁定的。

特別的,這個fragment可以通過getActivity()訪問Activity實例,并且輕松的執(zhí)行如在activity布局中查找view一類的任務(wù)

    View listView = getActivity().findViewById(R.id.list);
  

同樣的,你的activity可以通過使用indFragmentById()或者findFragmentByTag()從FragmentManager獲得一個fragment引用從而調(diào)用fragment中的方法

    ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
  

?

建立activity的事件回調(diào)

In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary.

一些情況下,你也許需要一個fragment來與activity共享events。一個好的方式是在fragment內(nèi)部定義一個回調(diào)接口并且要求宿主activity實現(xiàn)它。當(dāng)activity通過這個接口收到一個回調(diào)時,如果需要的話他可以與其他在布局中的fragments分享信息。

For example, if a news application has two fragments in an activity—one to show a list of articles (fragment A) and another to display an article (fragment B)—then fragment A must tell the activity when a list item is selected so that it can tell fragment B to display the article. In this case, the OnArticleSelectedListener interface is declared inside fragment A:

例如:如果一個新聞應(yīng)用在一個activity有兩個fragment,一個用來顯示文章列表(fragment A),另一個用來顯示一篇文章(fragment B),那么當(dāng)列表的條目被選中的時候fragment A必須告訴activity,這樣才能通知fragment B來顯示此文章。在這個例子中,OnArticleSelectedListener接口在fragment A內(nèi)部聲明

    public static class FragmentA extends ListFragment {
    ...
    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
}
  

?

fragment的宿主activity實現(xiàn)OnArticleSelectedListener接口并且覆蓋onArticleSelected()用來通知fragment B來自fragment A的事件。

為了保證宿主activity實現(xiàn)這個接口,fragment A的onAttach()回調(diào)方法(當(dāng)往activity添加activity時由系統(tǒng)調(diào)用)通過強制轉(zhuǎn)換傳入onAttach()方法的Activity,實例化一個OnArticleSelectedListener實例。

    public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...
}
  

?

如果activity沒有實現(xiàn)這個接口,那么fragment會拋出ClassCastException異常。如果成功,mListener成員變量會持有一個實現(xiàn)了OnArticleSelectedListener接口的activity引用,那么fragment A可以通過activity中調(diào)用OnArticleSelectedListener接口定義好的方法來分享event。例如:如果fragment A是ListFragment的一個擴(kuò)展,每次用戶點擊列表條目的時候,系統(tǒng)調(diào)用fragment的onListItemClick()方法,其中fragment調(diào)用onArticleSelected()來與activity分享event

    public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(noteUri);
    }
    ...
}
  

?

向Action Bar添加條目

你的fragment可以貢獻(xiàn)menu條目到activity的Options Menu通過實現(xiàn)onCreateOptionsMenu()。為了讓這個方法可以接收到調(diào)用,無論怎樣,在onCreate()期間,你必須調(diào)用setHasOptionsMenu()來告訴fragment愿意為Options Menu添加條目(否則,fragment不會收到onCreateOptionsMenu()的調(diào)用)

Any items that you then add to the Options Menu from the fragment are appended to the existing menu items. The fragment also receives callbacks to onOptionsItemSelected() when a menu item is selected.

任何從fragment追加到已經(jīng)存在的menu條目上的條目添加到Options Menu中。當(dāng)一個menu條目被選中時fragment也會收到onOptionsItemSelected()的回調(diào)

你也可以在你的fragment布局中注冊一個view通過調(diào)用registerForContextMenu()用來提供一個context menu。 當(dāng)用戶打開context menu時,fragment會收到一個onCreateContextMenu()調(diào)用。當(dāng)用戶選中一個條目時,fragment會收到一個onContextItemSelected()的調(diào)用。

Note: Although your fragment receives an on-item-selected callback for each menu item it adds, the activity is first to receive the respective callback when the user selects a menu item. If the activity's implementation of the on-item-selected callback does not handle the selected item, then the event is passed to the fragment's callback. This is true for the Options Menu and context menus.

注意:盡管fragment的每一個添加的menu條目都會收到一個on-item-selected回調(diào),但activity會首先獲得相應(yīng)的回調(diào)。如果activity中on-item-selected回調(diào)的實現(xiàn)沒有處理選中的item,那么event會傳遞給fragment的回調(diào)。對于Options Menu和context menus都是如此。

?

處理Fragment聲明周期

管理fragment的聲明周期與管理activity的聲明周期很像。和activity一樣,fragment可以存在于3中狀態(tài):

Resumed

fragment在運行中的activity可見

Paused

另一個activity運行并聚焦在前臺,但是含有fragment的activity仍然是可見的(在上面的activity是部分透明的或者他沒有覆蓋整個屏幕)。

Stopped

The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity's process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment's onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated().

fragment是不可見的。宿主activity被stopped,或者fragment被從activity中remove掉但是添加到了back stack中。一個stopped的fragment仍然處于活著的狀態(tài)(所有的狀態(tài)和成員信息被系統(tǒng)保存著)。不管怎樣,它對用戶來說不再可見,并且activity被killed的時候fragment也隨之被kill掉。和activity一樣,你可以用Bundle保存fragment的狀態(tài),萬一activity的進(jìn)程被kill掉并且當(dāng)activity重新create的時候你需要恢復(fù)fragment的狀態(tài)。 你可以在fragment的onSaveInstanceState()調(diào)用期間保存狀態(tài),在onCreate()、onCreateView()或onActivityCreated()期間恢復(fù)其狀態(tài)。

?

The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that's managed by the system when it's stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.

activity與fragment的聲明周期最重要的區(qū)別是如何保存到他們各自的back stack中。當(dāng)activity stopped的時候,它放置到由系統(tǒng)管理的activities的back stack中,默認(rèn)地(所以用戶可以通過back按鍵導(dǎo)航回去,如 Tasks and Back Stack 中討論的一樣)。僅當(dāng)你明確的要求在移除fragment的事務(wù)期間,通過調(diào)用addToBackStack()保存這個fragment實例時,fragment才會被放置到一個有宿主activity管理的back stack中。

Otherwise, managing the fragment lifecycle is very similar to managing the activity lifecycle. So, the same practices for managing the activity lifecycle also apply to fragments. What you also need to understand, though, is how the life of the activity affects the life of the fragment.

其他方面,管理fragment的聲明周期與管理activity的聲明周期十分相似。所以與管理activity聲明周期相同的練習(xí)也適用于fragments。你需要理解的是:activity的生命如何影響到fragment的生命

?

?

Coordinating with the activity lifecycle

與activity生命周期的協(xié)調(diào)

The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment, such that each lifecycle callback for the activity results in a similar callback for each fragment. For example, when the activity receives onPause(), each fragment in the activity receives onPause().

fragment宿主activity的聲明周期直接影響到fragment生命周期,以至于對每一個fragment,activity每一個生命周期的回調(diào)導(dǎo)致一個相似的回調(diào)。例如:當(dāng)activity收到onPause()時,activity中的每一個fragment也會收到onPause()

Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are:

Fragments有一些附加的生命周期回調(diào) ,他們處理獨有的與activity的交互,為了執(zhí)行如建立和銷毀fragment的UI一類的action。這些附件的回調(diào)方法有:

onAttach()

Called when the fragment has been associated with the activity (the Activity is passed in here).

onCreateView()

Called to create the view hierarchy associated with the fragment.

onActivityCreated()

Called when the activity's onCreate() method has returned.

onDestroyView()

Called when the view hierarchy associated with the fragment is being removed.

onDetach()

Called when the fragment is being disassociated from the activity.

?


android Fragment開發(fā)文檔翻譯 - 2

you can see how each successive state of the activity determines which callback methods a fragment may receive. For example, when the activity has received its onCreate() callback, a fragment in the activity receives no more than the onActivityCreated() callback.

可以看到每一個activity依次的狀態(tài)如何決定fragment會收到哪一個函數(shù)的回調(diào)。

當(dāng)activity收到他的onCreate()回調(diào)時,activity中的fragmet至多會收到onActivityCreated()的回調(diào)

Once the activity reaches the resumed state, you can freely add and remove fragments to the activity. Thus, only while the activity is in the resumed state can the lifecycle of a fragment change independently.

一旦activit到了resumed狀態(tài),你可以隨意在activity中的添加和刪除fragment。 僅當(dāng)這個activity是處于resumed狀態(tài)下fragment的生命周期才可以獨立的改變。

However, when the activity leaves the resumed state, the fragment again is pushed through its lifecycle by the activity.

無論怎樣,當(dāng)activity離開resumed狀態(tài)時,fragment又一次被activity擠入它的下一個生命周期。

?

?

?

原文地址如下,英文水平實在有限,希望拍磚同時能給予指正。

http://developer.android.com/guide/topics/fundamentals/fragments.html

?

?

轉(zhuǎn)貼請保留以下鏈接

本人blog地址

http://su1216.iteye.com/

http://blog.csdn.net/su1216/

android Fragment開發(fā)文檔翻譯 - 2


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本最新免费二区 | 狠狠狠色丁香婷婷综合久久88 | 奇米影视狠狠狠天天777 | 中文字幕日本精品一区二区三区 | 99热久久这里只精品 | 精品黑人一区二区三区 | 亚洲精品中文字幕第一区 | 一区二区三区视频观看 | 黄色在线视频网 | 干一干操一操 | 日韩中文字幕在线观看视频 | 亚洲精品美女久久久久网站 | 亚洲激情一区 | 欧美亚洲国产精品久久久 | 国产91av视频| 免费一级毛片不卡在线播放 | 一区二区3区免费视频 | 日韩一区二区超清视频 | 婷婷中文网 | 手机在线中文字幕 | 久久精品国产免费中文 | 中文一级毛片 | 成人网在线视频 | 美国毛片免费观看 | 国产精品成人扳一级aa毛片 | 亚洲干综合 | 日本一区二区在线视频 | 久久草在线播放 | 欧美成人另类69 | 好色婷婷| 日本人69视频jizz免费看 | 亚洲精品久一区 | 久久久久久穴 | 四虎影视永久在线观看 | 国产精品久久久久乳精品爆 | 在线视频不卡国产在线视频不卡 | 国产精品视频男人的天堂 | 久热这里只精品99re8久 | 四虎永久在线观看视频精品 | 中文字幕在线精品不卡 | 色四虎|