Android Api Component---翻譯Fragment組件(二),androidfragment

來源:互聯網
上載者:User

Android Api Component---翻譯Fragment組件(二),androidfragment

我們接著上一篇翻譯吧Android Api Component---翻譯Fragment組件(一)


與activity通訊

儘管一個Fragment獨立於一個Activity作為一個對象被實現並且在多個activity中被使用,給定的fragment執行個體綁定到了包含它的那個activity中。


特別的是,這個fragment使用getActivity()可以訪問activity執行個體並且容易的執行像在activity布局中尋找一個視圖的任務:

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


同樣的,你的activity通過從FragmentManager中請求一個到Fragment的映射可以調用fragment中的方法,使用findFragmentById()或者findFragmentByTag()。例如:

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


建立事件回調到activity

在一些例子中,你也許需要一個fragment跟那個activity共用事件。一種好的方式是在fragment中定義一個回調介面,並且要求主activity實現它。當activity通過這個介面接收一個回調的時候,當需要的時候,它可以跟其它的fragment共用資訊。


例如,如果一個新聞應用程式在activity中有兩個fragment-一個是展示文章的列表(fragment A),另一個是展示一個文章(fragment B),那麼當一個清單項目被選中的時候,這個fragment必須告訴這個activity來告訴fragment B顯式這個文章。在這個例子中,fragment A中定義了介面OnArticaleSelectedListener:

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


然後fragment的主activity實現了OnArticleSelectedListener介面並且覆蓋了onArticleSelected()來通知來自於fragment A的事件給fragment B。為了確保主activity實現了這個介面,fragment A的onAttach()(當framgment添加到activity的時候,系統會調用這個方法)回調方法通過映射Activity到onAttach()中來初始化一個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還沒有實現這個介面,那麼fragment會拋出一個ClassCastException異常。成功的關鍵是mListener成員持有一個映射到activity的OnArticleSelectedListener的實現,為的是fragment A可以通過調用被定義在OnArticleSelectedListener介面中的方法來和這個activity共用事件。例如,如果fragment A是一個ListFragment的擴充,使用者每次點擊一個清單項目的時候,系統都會在fragment中調用onListItemClick(),這個方法可以調用onArticleSelected()來和這個activity共用事件:

public static class FragmentA extends ListFragment {    OnArticleSelectedListener mListener;    ...        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);    }    ....}


傳遞給onItemClick()的id參數是被點擊的項的行ID,這個ID是用來讓activity(或者其它fragment)從應用程式的ContentProvider中抓取文章用的。

關於

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.