【Android Training - 03】使用Fragments建立动态的UI [ Lesson 4 - Fragment之间的通信 ]
2012-11-28 22:33
531 查看
为了重用fragment的UI组件,你必须为每个fragment建立自己的container,模块化自己的layout与行为。一旦你定义了那些可重用的fragment,你可以使用activity与他们建立联系,对那些UI组件做组合动作等。
通常,你也会想要fragment-s之间能够交流。例如,基于用户事件来改变内容。所有的Fragment-to-Fragment之间的交互都是基于activity进行操作的。两个fragment之间没有办法直接交互。
为了允许fragment与activity进行交互,你可以在fragment中定义一个interface,然后在activity中去implement它。Fragment在它的onAttach()方法里面捕获接口的实现,然后call接口的方法来与activity进行交互。
下面是一个fragment与activity进行交互的例子:
那么现在,fragment可以通过
例如:下面在fragment中的方法会在用户点击listitem的时候被call到。Fragment使用这个callback接口来传递事件给父activity。
为了接受到fragment返回事件的callback,包含那个fragment的activity必须实现定义在fragment中的接口。
例如:下面的activity实现了上面例子中的接口。
宿主activity可以通过
fragment的public方法来传递msg给fragment。
如下演示了使用另外一个fragment来显示从上面例子的callback中获取到的内容。
转载请注明出自:http://blog.csdn.net/kesenhoo,谢谢!
通常,你也会想要fragment-s之间能够交流。例如,基于用户事件来改变内容。所有的Fragment-to-Fragment之间的交互都是基于activity进行操作的。两个fragment之间没有办法直接交互。
Define an Interface [定义接口]
为了允许fragment与activity进行交互,你可以在fragment中定义一个interface,然后在activity中去implement它。Fragment在它的onAttach()方法里面捕获接口的实现,然后call接口的方法来与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可以通过
onArticleSelected()传递messages给activity。这个方法去使用OnHeadlineSelectedListener接口的
mCallback
例如:下面在fragment中的方法会在用户点击listitem的时候被call到。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); }
Implement the Interface [实现接口]
为了接受到fragment返回事件的callback,包含那个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 } }
Deliver a Message to a Fragment [传递msg给fragment]
宿主activity可以通过findFragmentById()捕获到fragment实例,然后call
fragment的public方法来传递msg给fragment。
如下演示了使用另外一个fragment来显示从上面例子的callback中获取到的内容。
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(); } } }学习自:https://developer.android.com/training/basics/fragments/communicating.html,欢迎一起交流!
转载请注明出自:http://blog.csdn.net/kesenhoo,谢谢!
相关文章推荐
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 4 - Fragment之间的通信 ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 2 - 新建一个Fragment ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 2 - 新建一个Fragment ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 3 - 创建灵活可变的UI ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 1 - 使用Support Library ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 1 - 使用Support Library ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 3 - 创建灵活可变的UI ]
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 0 - 章节概览 ]
- Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信
- Android官方培训课程学习(三): 使用Fragment建立动态UI
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之构建灵活的UI
- Training--使用Fragment创建动态UI(3)-- 与其他Fragment通信
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之Fragment创建
- Android Training - 使用碎片创建一个动态UI(4) - 与其他碎片通信
- Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之与其它Fragment通信
- ANDROID中使用开源框架EVENTBUS3.0实现FRAGMENT之间的通信交互
- Training--使用Fragment创建动态UI(1)-- 创建一个Fragment
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Android中使用开源框架EventBus3.0实现Fragment之间的通信交互