为了重用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
}
}
—————————————————————————————————————————
寄主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();
}
}
}
分享到:
相关推荐
Android Cookbook: Problems and Solutions for Android Developers by Ian F. Darwin English | 10 May 2017 | ASIN: B071VLZ9ZK | 838 Pages | AZW3 | 12 MB Jump in and build working Android apps with the ...
Manning RxJava for Android Developers Manning RxJava for Android Developers
Kotlin for android developers 中文学习文档 https://leanpub.com/kotlin-for-android-developers 英文版 - Github: https://github.com/wangjiegulu/kotlin-for-android-developers-zh
Kotlin for android developers中文版翻译 Kotlin for android developers中文版翻译
本书介绍Scala基本语法、构造。以及如何将您已经知道的复制java语法转换为Scala,了解Scala除了javabecome之外还提供了哪些功能编程概念和惯用技巧和建议,这些技巧和建议在将现有java项目转换为Scala时非常有用
Linux for Developers: Jumpstart Your Linux Programming Skills (Developer's Library) by William "Bo" Rothwell English | 17 Apr. 2017 | ASIN: B06ZY85CPJ | 224 Pages | AZW3 | 5.37 MB Linux for ...
CalendarProviderAPI允许你在日历,事务,参与者,提醒等,执行查询,插入,更新,和删除操作。CalendarProvderAPI能通过应用程序和同步Adapter被使用。基于程序的类型的规则变化执行调用。这个文档主要集中于和一个...
kotlin for android developers 中文版, kotlin for android developers 中文版
《Kotlin for android developers中文版翻译》
Wiley - Java Programming for Android Developers - For Dummies 2nd - 2017
Kotlin-for-Android-Developers:该书的配套应用
Kotlin for android developers(非扫描中文)下载后解压即可,更多请关注我的博客:xiayye5
Kotlin for Android Developers 中文版 ---------------------------------------------------- 本 ePub 基于开源文档,目录书签齐全。 版权归原作者,翻译版权归译者。 ------------------------------------------...
kotlin-for-android-developers-zh 中文版本,请支持正版。 https://leanpub.com/kotlin-for-android-developers
Kotlin for Android Developers - Learn Kotlin the easy way.pdf
kotlin-for-android-developers; kotlin-for-android-developers; kotlin-for-android-developers;
This book is a handy reference for all Android app developers. Table of Contents Chapter 1: Getting Started with Android Chapter 2: Views, Graphics, and Drawing Chapter 3: User Interaction Recipes ...
Master the fundamentals of Scala and understand its emphasis on functional programming that sets it apart from ...Java developers looking to transition to Scala. No prior experience necessary in Scala.