Fragment间通信
来源:互联网 发布:php获取qq信息 编辑:程序博客网 时间:2024/06/06 01:40
为了重用Fragment UI组件,您应该将每个组件构建为一个完全自包含的模块化组件,以定义其自己的布局和行为。一旦定义了这些可重用的Fragment,您可以将它们与一个Activity相关联,并将它们与应用程序逻辑连接,以实现整个复合UI。
通常,您将需要一个Fragment与另一个Fragment进行通信,例如根据用户事件更改内容。所有Fragment到Fragment的通信通过相关联的Activity完成。两个Fragment永远不应该直接通信。
定义接口
要允许Fragment与其Activity进行通信,您可以在Fragment类中定义一个接口,并在Activity中实现它。 Fragment在其onAttach()
生命周期方法期间捕获接口实现,然后可以调用Interface方法以与Activity进行通信。
这里是Fragment to 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,然后直接调用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(); } }}
- Fragment间的通信
- Fragment 间的通信
- Fragment间的通信
- Fragment间的通信
- Fragment间进行通信
- Android Fragment 间通信
- Fragment间的通信
- Fragment间通信
- Fragment间的通信
- Android Fragment间通信
- Fragment & Fragment间的通信实现
- Fragment 通信
- fragment fragment 通信
- activity间的通信,fragment间的通信,fragment与activity间的通信
- Android培训---Fragment间的通信
- 观察者模式与Fragment间通信
- fragment及fragment的通信
- fragment嵌套fragment的通信
- Canada Cup 2016 D. Contest Balloons详解(贪心+优先队列)
- mysql ---创建简单的数据库
- Android监听网络变化
- CSS3 中的取值和单位
- Android7.0 多窗口特性
- Fragment间通信
- D3D11教程三十之ProjectiveTexturing(投影纹理)
- Python中的 __all__和__path__ 解析
- HDU 4135:Co-prime (容斥原理)
- 怎么参与到百度的开源项目中来
- PHP中关于composer那点事
- 静态函数和静态成员
- VS下用C++实现的简单3D射击游戏(附游戏文件与源工程代码)
- 校验码(奇偶码、海明码和循环冗余码)图片版