《Training:Communicating with Other Fragments》
来源:互联网 发布:mac浏览器flash插件 编辑:程序博客网 时间:2024/06/05 07:25
注:本人非专业翻译人员,编写此博客只是为了自学使用,如有疑问,请阅读官方文档。
为了重用Fragment UI组件,您应该将每个构建为完全独立的模块化组件,以定义其自身的布局和行为。 一旦定义了这些可重用的碎片,就可以将它们与Activity进行关联,并将它们与应用程序逻辑连接起来,以实现整体复合UI。
通常,您将需要一个碎片与另一个碎片通信,例如,根据用户事件更改内容。 所有碎片到碎片的通信都是通过相关的Activity完成的。 两个碎片不应该直接通信。
一、定义界面
要允许碎片与其活动通信,您可以在Fragment类中定义一个接口,并在Activity中实现。 碎片在其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"); } } ...}
现在,片段可以使用OnHeadlineSelectedListener接口的mCallback实例调用onArticleSelected()方法(或接口中的其他方法)将消息传递给活动。
例如,当用户单击列表项时,调用片段中的以下方法。 该片段使用回调接口将事件传递给父活动。
@Override public void onListItemClick(ListView l, View v, int position, long id) { // Send the event to the host activity mCallback.onArticleSelected(position); }
二、实现接口
为了从碎片接收事件回调,承载它的活动必须实现碎片类中定义的接口。
例如,以下活动实现了上述示例中的接口。
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 }}
三、向碎片传递消息
主机活动可以通过使用findFragmentById()捕获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
- 《Training:Communicating with Other Fragments》
- Communicating with Other Fragments
- Communicating with Other Fragments
- Android — Communicating with Other Fragments
- Building a Dynamic UI with Fragments - Communicating with Other Fragments
- 11Communicating with Other Fragments(Fragment之间的通信)
- Fragment间通信传递数据 Communicating with Other Fragments
- Building a Dynamic UI with Fragments ---- Communicating with Other Fragme (三)
- unit 5 Communicating with other users
- Communicating with the Debugger
- Develop--Training(三)Getting Started--Interacting with Other Apps
- Fragment Communicating with the Activity
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Creating a Fragment) (一)
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Building a Flexible UI) (二)
- 阅读Android官方教程 Google Training 1.4 ----- Building a Dynamic UI with Fragments
- Android Develop Training——和其它Android应用交互(Interacting with Other Apps)
- Communicating with the Database (Using ADO)
- communicating-with-serial-port-in-C-Sharp
- MySQL数据库水平分表策略--一致性hash
- Java强引用、 软引用
- NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED、NUMBEROF_RETRIES_EXCEEDED
- DNS host区别与设置
- opencv中typedef const _InputArray& InputArray
- 《Training:Communicating with Other Fragments》
- spring-mybatis集成的xml配置详解,
- 得到微信OPENID方式
- 人工智能综述
- visual studio fatal error C1083: 无法打开源文件
- java csript定时器函数 setTimeout()【新手】
- 《数据结构之链表篇》--简单通讯录的C++实现
- 123
- 十万太短,三万太长