Android Fragment之间相互通信

Android中Fragment之间的通信只能借助它们所属的Activity进行通信,两个Fragment之间是不能直接进行通信的。那么如何才能让两个Fragment之间进行通信呢?答案是通过接口进行通信。

a)自定义接口

在Fragment中定义一个接口,然后让Activity实现这个接口,Fragment可以在attach方法中捕获这个接口,然后调用接口里面的方法。代码如下:

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可以与Activity进行通信了,通过定义在OnHeadlineSelectedListener 中的onArticleSelected()方法。比如在fragment的列表中,点击列表,我们可以回调这个方法,将事件传递给Activity。代码如下所示:

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

下面看一下activity是如何实现OnHeadlineSelectedListener 并且是将信息是如何传递给另外一个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();
        }
    }
}

通过FragmentManager 的findFragmentById方法,Activity可以得到另外一个Fragment的实例,这样就可以将从一个Fragment得到的position的数据传递给另外一个Fragment了。



你可能感兴趣的:(Android Fragment之间相互通信)