欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Android UI開(kāi)發(fā)第二十六篇——Fragment間的通信

為了重用Fragment的UI組件,創(chuàng )建的每個(gè)Fragment都應該是自包含的、有它自己的布局和行為的模塊化組件。一旦你定義了這些可重用的Fragment,你就可以把它們跟一個(gè)Activity關(guān)聯(lián),并把它們跟應用程序的邏輯相連來(lái)實(shí)現全部的組合式UI。

       現實(shí)中我們經(jīng)常想要一個(gè)Fragment跟另一個(gè)Fragment進(jìn)行通信,例如,要基于一個(gè)用戶(hù)事件來(lái)改變內容。所有的Fragment間的通信都是通過(guò)跟關(guān)聯(lián)的Activity來(lái)完成的。另個(gè)Fragment不應該直接通信。也就是說(shuō)Fragment間不直接通信,通過(guò)Activity轉一下,按java常規,轉一下多是使用Interface實(shí)現的。

定義Interface

       為了讓Fragment跟它的Activity通信,你可以在Fragment類(lèi)中定義一個(gè)接口,并在它所屬的Activity中實(shí)現該接口。Fragment在它的onAttach()方法執行期間捕獲該接口的實(shí)現,然后就可以調用接口方法,以便跟Activity通信。

以下是Fragment跟Activity通信的示例:

  1. public class HeadlinesFragment extends ListFragment {  
  2.     OnHeadlineSelectedListener mCallback;  
  3.   
  4.     // Container Activity must implement this interface  
  5.     public interface OnHeadlineSelectedListener {  
  6.         public void onArticleSelected(int position);  
  7.     }  
  8.   
  9.     @Override  
  10.     public void onAttach(Activity activity) {  
  11.         super.onAttach(activity);  
  12.           
  13.         // This makes sure that the container activity has implemented  
  14.         // the callback interface. If not, it throws an exception  
  15.         try {  
  16.             mCallback = (OnHeadlineSelectedListener) activity;  
  17.         } catch (ClassCastException e) {  
  18.             throw new ClassCastException(activity.toString()  
  19.                     + " must implement OnHeadlineSelectedListener");  
  20.         }  
  21.     }  
  22.       
  23.     ...  
  24. }  

      現在,這個(gè)Fragment就可以通過(guò)調用OnHealdlineSelectedListener接口實(shí)例mCallback的onArticleSelected()方法(或其他的接口中的方法)給Activity發(fā)送消息。

例如,在Fragment中的下列方法會(huì )用戶(hù)點(diǎn)擊列表項時(shí)被調用。該Fragment使用回調接口把該事件發(fā)送給它的父Activity。

  1. @Override  
  2.    public void onListItemClick(ListView l, View v, int position, long id) {  
  3.        // Send the event to the host activity  
  4.        mCallback.onArticleSelected(position);  
  5.    }  


實(shí)現Interface

為了從Fragment中接收事件回調,包含Fragment的Activity必須實(shí)現Fragment類(lèi)中定義的接口。

例如,下面Activity實(shí)現了上面示例中定義的接口:

  1. public static class MainActivity extends Activity  
  2.         implements HeadlinesFragment.OnHeadlineSelectedListener{  
  3.     ...  
  4.       
  5.     public void onArticleSelected(int position) {  
  6.         // The user selected the headline of an article from the HeadlinesFragment  
  7.         // Do something here to display that article  
  8.     }  
  9. }  



把消息傳遞給另一個(gè)Fragment

         通過(guò)使用findFragmentById()方法捕獲Fragment實(shí)例,宿主Activity可以把消息發(fā)送給該Fragment,然后直接調用該Fragment的公共方法。

        例如,上面的示例,Activty通過(guò)Interface的實(shí)現方法,傳遞數據到另一個(gè)Fragment。

  1. public static class MainActivity extends Activity  
  2.         implements HeadlinesFragment.OnHeadlineSelectedListener{  
  3.     ...  
  4.   
  5.     public void onArticleSelected(int position) {  
  6.         // The user selected the headline of an article from the HeadlinesFragment  
  7.         // Do something here to display that article  
  8.   
  9.         ArticleFragment articleFrag = (ArticleFragment)  
  10.                 getSupportFragmentManager().findFragmentById(R.id.article_fragment);  
  11.   
  12.         if (articleFrag != null) {  
  13.             // If article frag is available, we're in two-pane layout...  
  14.   
  15.             // Call a method in the ArticleFragment to update its content  
  16.             articleFrag.updateArticleView(position);  
  17.         } else {  
  18.             // Otherwise, we're in the one-pane layout and must swap frags...  
  19.   
  20.             // Create fragment and give it an argument for the selected article  
  21.             ArticleFragment newFragment = new ArticleFragment();  
  22.             Bundle args = new Bundle();  
  23.             args.putInt(ArticleFragment.ARG_POSITION, position);  
  24.             newFragment.setArguments(args);  
  25.           
  26.             FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
  27.   
  28.             // Replace whatever is in the fragment_container view with this fragment,  
  29.             // and add the transaction to the back stack so the user can navigate back  
  30.             transaction.replace(R.id.fragment_container, newFragment);  
  31.             transaction.addToBackStack(null);  
  32.   
  33.             // Commit the transaction  
  34.             transaction.commit();  
  35.         }  
  36.     }  
  37. }  


Fragment中使用左右滑動(dòng)菜單 中應用到了Fragment間的通信


參考:http://developer.android.com/training/basics/fragments/communicating.html

/**
* @author 張興業(yè)
* 郵箱:xy-zhang#163.com
* android開(kāi)發(fā)進(jìn)階群:278401545
*
*/

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Android入門(mén)之Fragment用法
Android Fragment 基本介紹
Android中的MVP | Rocko's blog
Fragment對比Activity -Android碎片介紹
使用Activity類(lèi)的runOnUiThread執行線(xiàn)程更新UI操作
14. ViewPager + Fragment 實(shí)現左右滑動(dòng)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久