Android training(1)-Building a Dynamic UI with Fragments(用碎片创造一个动态的UI)(C)
2015-04-30 14:05
459 查看
C:Fragment之间的通信
为了可以重复利用这些Fragment组件,你必须把他们创建成完全独立的、模块化的组件,并且拥有自己的布局和行为。你定义了这些可以复用的fragments以后,你就可以把他们和Activity联系在一起,并且根据应用的逻辑来连接他们实现全部的复合界面。
经常的,你的fragments之间需要相互通信,例如基于用户事件改变内容,所有fragments之间的通信都必须通过关联他们的Activity,两个Fragments之间绝对不能直接通信。
<
4000
span style="font-size:18px;">C.a 定义一个接口
为了允许一个Fragment向它的Activity通信,你可以在你的fragment中定义一个接口,并且在Activity中实现这个接口。fragment在它的onAttach()生命周期方法期间获得到接口的实现,接着就可以调用接口的方法来实现与Activity的通信了。
下面就是一个Fragment与Activity通信的例子:
现在fragment可以传递信息通过调用onArticleSelected()方法(或者接口中的其他方法),使用的是OnHeadlineSelectedListener接口的实例mCallback。
例如,下面fragment中的方法在用户点击列表中的一项时,fragment使用回调接口来把这个事件传递给Activity。
C.b 实现这个接口
为了从Fragment收到回调的信息,Activity必须实现在fragment中定义的接口。
例如:下面的这个Activity实现上面定义的这个接口。
C.c 传递信息到Fragment
Activity可以传递信息给在它里面的fragment,通过findFragmentById()获得Fragment实例,然后直接调用fragment的公共方法。
例如,假设上面的Activity包含另外的一个fragment,用来显示被上面的回调方法返回的信息所控制的具体的项,在这种情况下,Activity可以传递通过回调方法返回的信息给那个用来显示这条信息详情的fragment。
为了可以重复利用这些Fragment组件,你必须把他们创建成完全独立的、模块化的组件,并且拥有自己的布局和行为。你定义了这些可以复用的fragments以后,你就可以把他们和Activity联系在一起,并且根据应用的逻辑来连接他们实现全部的复合界面。
经常的,你的fragments之间需要相互通信,例如基于用户事件改变内容,所有fragments之间的通信都必须通过关联他们的Activity,两个Fragments之间绝对不能直接通信。
<
4000
span style="font-size:18px;">C.a 定义一个接口
为了允许一个Fragment向它的Activity通信,你可以在你的fragment中定义一个接口,并且在Activity中实现这个接口。fragment在它的onAttach()生命周期方法期间获得到接口的实现,接着就可以调用接口的方法来实现与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()方法(或者接口中的其他方法),使用的是OnHeadlineSelectedListener接口的实例mCallback。
例如,下面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); }
C.b 实现这个接口
为了从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 } }
C.c 传递信息到Fragment
Activity可以传递信息给在它里面的fragment,通过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(); } } }
相关文章推荐
- Android training(1)-Building a Dynamic UI with Fragments(用碎片创造一个动态的UI)(B)
- Android training(1)-Building a Dynamic UI with Fragments(用碎片创造一个动态的UI)(A)
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Building a Flexible UI) (二)
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Creating a Fragment) (一)
- AndroidTraining学习------Building-a-Dynamic-UI-with-Fragments-
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 2 - 新建一个Fragment ]
- Training—Building a Dynamic UI with Fragments
- Android Training - 使用碎片创建一个动态UI(2) - 创建一个碎片
- Android Training - 使用碎片创建一个动态UI(4) - 与其他碎片通信
- android-Building a Dynamic UI with Fragments
- Android Training - 使用碎片创建一个动态UI
- 【English】Android -> Training -> Building a Dynamic UI with Fragment -> create a Fragment
- Android Training - 使用碎片创建一个动态UI
- Android Training - 使用碎片创建一个动态UI(3) - 构建一个灵活的UI
- 【Android Training - 03】使用Fragments建立动态的UI [ Lesson 2 - 新建一个Fragment ]
- android开发官方教程之Building a Dynamic UI with Fragments
- Android Training - 使用碎片创建一个动态UI(1) - 使用android支持库
- (四)Building a Dynamic UI with Fragments使用fragment构建动态用户界面
- Building a Dynamic UI with Fragments
- Building a Dynamic UI with Fragments