Fragments之间的交互
来源:互联网 发布:光纤网络是哪个公司的 编辑:程序博客网 时间:2024/05/16 12:40
为了重用Fragment UI组件,你应该把每一个fragment都构建成完全的自包含的、模块化的组件,定义他们自己的布局与行为。当你定义好这些模块化的Fragments的时,你就可以让他们关联acticvity,使他们与application的逻辑结合起来,实现全局的复合的UI。
经常地,你想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); }
实现接口
为了接收回调事件,宿主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的public方法来向fragment传递消息。
例如,想象一下,上面所示的activity可能包含另外一个fragment,这个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
- Fragments之间的交互
- Fragments之间的交互
- Fragments之间的交互
- Android开发之Fragments之间的交互
- Fragments之间的交互(实现参数传递)
- 【Android 开发教程】Fragments间的交互
- Android fragments实例之间的通信
- Android fragments实例之间的通信
- 应用程序之间的交互
- Activity 之间的交互
- Fragment之间的交互
- Activity之间的交互
- 11Communicating with Other Fragments(Fragment之间的通信)
- Android Fragments 与 Activity 生命周期之间的关系
- Fragment之Fragments之间的通信(3)
- Android组件之间的交互
- Android APP之间的交互
- Fragment之间的实时交互
- 数字分隔(二)
- 2009年9月全国计算机等级考试四级笔试试卷网络工程师
- BZOJ 2730 HNOI2012 矿场搭建 Tarjan
- 挣值分析:预测技术
- POJ 2002 Squares 计算集合 点的hash
- Fragments之间的交互
- Android 调试 中 conversion to dalvik format failed with error 1的解决办法汇总
- iOS 多线程的理解
- 日语学习之新版初级标准日本语-20 スミスさんはピアノを弾くことができます
- 浅谈C++多态性
- void pointer
- find the longest of the shortest (hdu 1595 SPFA+枚举)
- 《Linux设备驱动程序》——调试技术
- grunt-restful-mock