原文地址:https://developer.android.com/training/basics/fragments/communicating.html
-------------------------------------------------------------------------------------------------------------------------
In order to reuse the Fragment UI components, you should build each as a completely self-contained, modular component that defines its own layout and behavior. Once you have defined these reusable Fragments, you can associate them with an Activity and connect
them with the application logic to realize the overall composite UI.
为了重用Fragment UI部件,你应该每一个建成完全独立的、模块化的部件,定义了它自己的布局和行为。一旦你已经定义了这些可重用的Fragments,你可以将它们与一个Activity相关联,并将它们和应用逻辑相连接来实现整体复合的UI。
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
一旦你想要让一个Fragment和另一个进行交流,例如基于用户时间改变内容。所有的Fragment-to-Fragment交互通过相关联的Activity来完成。两个Fragment永远不应该直接交互。
Define an Interface —— 定义一个接口
To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the
Interface methods in order to communicate with the Activity.
为了允许一个Fragment和它的Activity进行交互,你可以在Fragment类中定义一个接口,然后在Activity中实现它。Fragment在它的onAttach()生命周期方法中捕捉这个接口的实现,并且之后可以调用接口方法来达到和Activity交互的目的。
Here is an example of Fragment to Activity communication:
下面是一个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");
}
}
...
}
Now the fragment can deliver messages to the activity by calling the onArticleSelected()
method (or other methods in the interface) using the
mCallback
instance of the OnHeadlineSelectedListener
interface.
现在,框架可以通过调用onArticleSelected()方法(或者其他接口中的方法)、使用OnHeadlineSelectedListener
接口的实例mCallback向activity交付信息。
For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.
例如,当用户在一个列表上点击时将调用下面的方法。框架使用回调函数接口来向父类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 —— 实现接口
In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.
为了从框架中接收事件回调函数,持有它的activity必须实现在框架类中定义的接口。
For example, the following activity implements the interface from the above example.
例如,下面的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 —— 向一个框架交付信息
The host activity can deliver messages to a fragment by capturing the Fragment
instance with
findFragmentById()
, then directly call the fragment's public methods.
主activity可以通过Fragment实例、使用findFragmentById()向框架交付信息,然后直接调用框架的公共方法。
For instance, imagine that the activity shown above may contain another fragment that's used to display the item specified by the data returned in the above callback method. In this case, the activity can pass the information received in the callback method
to the other fragment that will display the item:
例如,想象上面演示的activity可以包含另一个框架来显示上述回调函数返回的数据。在这种情况下,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
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是activity的模块化组件,它拥有自己的生命周期,接受它自己的输入事件,你也可以在运行activity的时候添加或者移除它(有点像“子activity”你可以在不同的activity中重用) 这节课演示怎么样使用...
使用Fragment如何创建高性能UI;使用Fragment如何创建高性能UI
We need a new approach, and fragments give us this new approach. In this book, you'll learn how to use fragments to meet the challenges of creating dynamic UIs in the modern world of mobile app ...
Create engaging apps with fragments to provide a rich user interface that dynamically adapts to the individual characteristics of your customers' tablets and smartphones About This Book From an ...
基于Android框架的动态UI构建(Creating Dynamic UI with Android Fragments, 2nd Edition)-2016英文原版,0积分——该书是2016年最新的第2版,全书154页。
Long gone are the days of the mobile apps with a static UI squished onto a tiny screen. Today's users expect mobile apps to be dynamic and highly interactive. They expect an app to look fantastic when...
Long gone are the days of mobile apps with a static UI squished on a tiny screen. Today's users expect mobile apps to be dynamic and highly interactive. They expect an app to look fantastic when they ...
Robust fragments-based tracking with adaptive feature selection
nested-fragments-master
对目标进行分多个块,利用积分直方图进行计算,用EMD比较,该方法能够对目标出现部分遮挡进行很好的跟踪
资源来自pypi官网。 资源全名:Agora-Fragments-0.1.1.tar.gz
[Packt Publishing] Creating Dynamic UI with Android Fragments E Book ☆ 图书概要:☆ Leverage the power of Android fragments to develop dynamic user interfaces for your apps Overview Learn ...
android的不断革新,是android的UI控件更加的人性化,Building a Dynamic UI with Fragments,动态的UI更加的增强了界面的交互
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
Material-Design-Tab-Layout-with-View-Pager-and-Fragments-Example-:带有“查看寻呼机”和“片段”的“材料设计”选项卡布局完整的源代码
Creating Dynamic UI with Android Fragments Leverage the power of Android Fragments to develop dynamic user interfaces for your apps
android-fragments-phoneTablet-example 练习解决方案 Coursera - 为 Android 手持系统编写移动应用程序:第 1 部分
- Render of a big sized image into fragments - Information on the aproximated size of the fragments - Render of single fragments - Automatic joining of the fragments - Compatible with Scanline ...