5.3 与其他Fragment交互

为了让Fragment UI部件能够被重用,Fragment被定义成自包含、模块化的部件,拥有自己的布局和行为。这些Fragment被创建后,可以通过应用逻辑将其与Activity或者其他Fragment相关联从而实现一个复合UI。

当需要一个Fragment与其他Fragment交互时,如基于用户事件的内容修改。所有的Fragment之间的通信都通过其关联的Activity,而不会直接跟对方Fragment通信。

定义接口

为了让Fragment能跟Activity通信,需要为Fragment定义一个接口,并在Activity调用该接口。Fragment在onAttach()生命周期中方法中获取到接口方法,之后就可以通过调用接口中的方法与Activity进行通信。下面展示了一个Fragment与Activity的通讯实现:

public classHeadlinesFragment extendsListFragment { 
   
OnHeadlineSelectedListener mCallback; 
 
   
// Container Activity must implement this interface 
   
public interfaceOnHeadlineSelectedListener { 
       
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 newClassCastException(activity.toString() 
                   
+ " must implement OnHeadlineSelectedListener"); 
       
} 
   
} 
     
   
... 
}

这样,Fragment就能通过定义为OnHeadlineSelectedListener接口的mCallback实例来调用onArticleSelected()(或者接口内其他方法)来向Activity传递消息。例如,下面的方法在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); 
   
}

实现接口

为了能够让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 
   
} 
}

向Fragment传递消息

Activity通过findFragmentById()获取Fragment实例,然后直接调用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(); 
        } 
    } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值