Android Developers:和其它Fragment通信

为了重用Fragment UI组件,你应该作为一个定义了它自己的布局和行为的,完全独立的,模块化的组建来构建。一旦你定义了这些可重用的Fragment,你使用一个Activity关联它们,和结合应用程序的逻辑以实现整体复合界面。 

 

经常你会想让一个fragment和另一个通信,例如基于用户事件改变内容。所有Fragment和Fragment的通信是通过相关的Activity完成。两个Fragment不能直接通信。 

 

定义一个接口 

————————————————————————————————————————————————————————————— 

为了允许fragment和它的activity通信,你可以在这个Fragment类中定义一个接口,并在这个Activity中实现它。这个fragment在它的onAttach()生命周期方法中获取接口的实现,然后调用这个接口方法和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能使用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); 
    } 

 

实现接口 

———————————————————————————————————————————————— 

为了从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(); 
        } 
    } 
} 

新技术,新未来!欢迎大家关注 “1024工场”微信服务号 ,时刻关注我们的最新的技术讯息! (甭客气!尽情的扫描或者长按!)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值