Fragment之Fragments之间的通信(3)

6 篇文章 0 订阅
3 篇文章 0 订阅
  • 一旦你定义这些可复用的Fragments,你可以将他们和一个Activity联系在一起,并且和应用的逻辑联系在一起来实现综合的UI。
  • 你可能希望一个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可以通过mCallback实例中的onArticleSelected()函数(或者接口中其他方法)向activity传递信息。
例如:当用户点击列表项时以下的fragment中的方法会被调用。fragment通过callback接口将事件传递给付activity

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

实现接口

为了能够接收fragment的事件回调,拥有该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传递消息
    设想一下,上面的activity可能包含另一个用来展示回调函数返回的具体数据的fragment。在这种情况下,activity可以将从回调函数中接收到的信息传递给另一个用来展示的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(); 
        } 
    } 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值