Android:片段之间的通信

来源:互联网 发布:恋爱久了没感觉了知乎 编辑:程序博客网 时间:2024/05/17 08:07

片段之间的通信

       为了重复使用片段UI组件,你应该将每个片段设计成一个完全独立的,模块化的组件,它有自己的布局和行为。一旦你定义了这些可重复使用的片段,你就能将它跟活动联系起来,并通过应用逻辑将他们连接,以组成全局的复合UI.

       很多时候,你可能希望几个片段之间来通信,例如根据用户的选择来改变内容。所有片段之间的通信都是通过跟他们想关联的活动来完成的,两个片段不能直接通信。

      

定义一个接口

       要实现片段跟活动之间的通信,你可以在片段类中定义一个接口,然后在活动中来实现这个接口。片段在它的onAttach()生命周期方法中来捕获对这个接口的实现,然后可以调用接口方法跟活动通信。

下面是一个可以跟活动通信的片段的例子:

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

        }

    }

   

    ...

}

现在,片段就能够像活动传递消息:通过OnHeadlineSelectedListener接口的mCallback实例来调用onArticleSelected()方法(或者是接口里面的其它方法)。

例如:当用户点击一个list选项时,片段内的如下方法被调用。片段利用这个回调接口传递事件到其活动中去。

@Override

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

        // Send the event to the host activity

        mCallback.onArticleSelected(position);

    }

实现这个接口

       为了获得片段的事件回调,片段的宿主活动必须实现片段里面定义的接口。

如下活动实现了前面所定义的接口:

               public static class MainActivity extends Activity

        implements HeadlinesFragment.OnHeadlineSelectedListener{

    ...

   

    public void onArticleSelected(Uri articleUri) {

        // The user selected the headline of an article from the HeadlinesFragment

        // Do something here to display that article

    }

}

 

向片段传递消息

       宿主活动通过findFragmentById()来捕获Fragment实例,然后直接调用Fragment的公开方法来传递信息。

       例如:加入上面的活动中包含例外一个片段,它所显示的内容是由第一个片段回调方法中传递的值所决定的。在这种情况下,活动就能把从回调方法中接收到的值传递给另外一个片段,然后显示正确的内容。

publicstaticclassMainActivityextendsActivity
        implementsHeadlinesFragment.OnHeadlineSelectedListener{
    ...
 
    publicvoid 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 =newArticleFragment();
            Bundle args =newBundle();
            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();
        }
    }
}

 

原创粉丝点击