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

【Android Developers Training】 22. 與其他fr

系統 2092 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。

原文鏈接: http://developer.android.com/training/basics/fragments/communicating.html


為了重用Fragment UI組件,你應該將每一個組件構建為自控地,模塊化的組件,它們有自己的布局和行為。一旦你定義好了這些可重用的fragment,你就可以將他們與activity關聯,通過應用邏輯將它們連接起來,以此實現一個整體復合的界面。

你經常會希望一個fragment可以和另一個通信,比如想要交換基于用戶事件的內容。所有的“fragment到fragment”的通信通過他們所關聯的activity來完成。兩個fragment永遠無法直接通信。

?

一). 定義一個接口

為了允許fragment向上與它隸屬的Activity通信,你可以在這個fragment中定義一個接口,然后再activity中實現。這個Fragment會在它的生命周期函數 onAttach()中 捕捉到接口的實現,而后就能調用接口方法來與activity通信。

下面是一個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"
      
        );

        }

    }

    

    ...

}
      
    

現在,fragment可以通過調用 onArticleSelected()方法(或其他接口中的方法)向 activity發送消息了。在此例中,使用的是 OnHeadlineSelectedListener接口的實例: mCallback。

比如:下面所示的fragment方法會在當用戶點擊了一個列表項時別調用。fragment使用回調接口將事件發送給父activity。

      
        @Override

    
      
      
        public
      
      
        void
      
       onListItemClick(ListView l, View v, 
      
        int
      
       position, 
      
        long
      
      
         id) {

        
      
      
        //
      
      
         Send the event to the host activity
      
      
                mCallback.onArticleSelected(position);

    }
      
    

?

二). 實現接口

為了接收來自fragment的事件回調,宿主activity必須實現在fragment類中所定義的接口。

下例中的activity實現了上例中的接口:

      
        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
      
      
            }

}
      
    

?

三). 將消息發送給Fragment

宿主Activity可以通過 findFragmentById() 來獲取fragment實例, 之后就能調用fragment的公有方法, 借此把消息傳遞給fragment。

舉一個例子:我們想象上述的Activity可能還容納了另一個fragment,它用來展示上述回調方法中歲返回特定項的數據。在這個例子中,activity可以把在回調方法中收到的信息發送給另一個fragment,這個fragment來顯示這些數據。

      
        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();

        }

    }

}
      
    

【Android Developers Training】 22. 與其他fragment通信


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日日操天天 | 欧美日本免费观看αv片 | 亚洲激情在线 | 免费国产一级特黄aa大片在线 | 九九九九热精品免费视频 | 日产国语一区二区三区在线看 | 色四月婷婷 | 欧美精品福利在线视频 | 欧美国产精品不卡在线观看 | 毛片看看| 亚洲国产精品久久久久久网站 | 国产乱子伦一级毛片 | 久草首页在线 | 欧美国产精品久久 | 另类婷婷 | 久久婷婷成人综合色 | 国内第一永久免费福利视频 | 麻豆伦理 | 国产成人毛片 | 女人洗澡一级毛片一级毛片 | 亚洲国产欧洲综合997久久 | 在线观看精品国内福利视频 | 老子影院午夜伦不卡手机 | 欧美猛交xxxxx | 热久久免费视频 | 99 久久99久久精品免观看 | 高h粗大强行撑开紧窄的嫩缝 | 91亚洲精品久久 | 96精品国产高清在线看入口 | 99久久这里只精品麻豆 | 天天天干干干 | 欧美日韩综合精品一区二区三区 | 玖玖爱免费 | 精品视频香蕉尹人在线 | 久操综合| 色婷婷精品免费视频 | 四虎免费影院ww4164h | 国产三级精品三级男人的天堂 | 成人淫片免费视频95视频 | 日本免费一区二区久久人人澡 | 欧美日韩国产高清一区二区三区 |