Fragment笔记
来源:互联网 发布:微信sdk数据统计接入 编辑:程序博客网 时间:2024/05/06 18:26
- All Fragment-to-Fragment communication is done through the associated Activity
- 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.
Demo:
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.
For example
@Override public void onListItemClick(ListView l, View v, int position, long id) { // Send the event to the host activity mCallback.onArticleSelected(position); }
test
0 0
- Fragment笔记
- Fragment笔记
- Fragment笔记
- fragment笔记
- Android Fragment学习笔记
- fragment的用法笔记
- android fragment学习笔记
- Android Fragment 生命周期笔记
- fragment的用法笔记
- 学习笔记 android fragment
- android Fragment 学习笔记
- 【安卓笔记】Fragment
- Fragment 学习笔记
- fragment相关(笔记)
- Android笔记 fragment通信
- fragment的用法笔记
- android笔记9-fragment
- Fragment学习笔记
- (转) TCP为什么要三次握手,不是两次四次?
- Python闭包的学习
- linux中container_of(ptr, type, member)的使用
- 互联网推送服务原理:长连接+心跳机制(MQTT协议)
- Android Studio系列教程四--Gradle基础
- Fragment笔记
- 清除浮动
- 【Opencv】opencv中实践机器学习KNN算法
- HDU2089经典的数论dp
- 懒惰的自己
- Socket编程总结!
- 2015大数据发展趋势与应用(外发版)
- 遇到的问题:Class file collision: A resource exists with a different case:
- 通俗理解频谱泄露