原文自:http://android.eoe.cn/topic/ui
Communicating with Other Fragments
为了重用Fragment UI组件,你应该将Fragment建立成完全独立,模块化并且定义了自己布局和行为的组件。一旦你定义了这些可重用的Fragment,你可以通过activity,应用程序逻辑使它们关联,交互以组成一个整体复合型UI。
通常情况下,你希望一个Fragment可以与另一个交互,比如基于用户事件去修改内容。所有Fragment到Fragment的交互都是通过相关联的activity来做的。两个fragment应该从不直接交互。
定义一个接口 Define an Interface
为了允许Fragment与它的activity交互,你可以在fragment类中定义一个接口并且在activity中实现它。fragment可以在生命周期中的onAttach()方法获取接口的实现并调用接口的方法与activity交互。
以下是fragment到activity的交互例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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了。
例如,当用户点击list item(list子项)时就会调用下面在fragment的方法。fragment使用回调接口提供事件到父的activity。
1 2 3 4 5 |
@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
为了接收来自fragment的事件回调,包含fragment的activity(你需要用来与fragment交互的activity)必须实现定义在fragment类中的接口。
例如:下面这个activity就实现了上一例子中的接口:
1 2 3 4 5 6 7 8 9 |
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(Uri articleUri) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
|
Deliver a Message to a Fragment
宿主activity可以使用findFragmentById()方法获取Fragment实例,然后直接调用fragment的公共方法提供信息给fragment。
例如,假设在上面显示的那个activity中可能包含另外一个fragment,并且用来显示由上面那个回调方法返回的数据指定的项目。在这个案例中,这个activity可以从回调函数中获得信息并且传递给其他显示项目的fragment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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();
}
}
}
|
相关推荐
例证了如何构建用户界面(第1章,GUI基础 - 构建一个接口),如何在屏幕上绘制矢量形状(第2章,图形 - Canvas),如何将用户交互与片段代码绑定(第3章,Widget事件- 绑定动作),以及与改善用户体验相关的其他组件...
提出一种视频编辑系统框架,以实现帧层次的交互式视频编辑。在此框架结构上可开发出其他基于帧的视频编辑工具。重点介绍了一个淡入淡出工具,可以对视频片段加入淡入淡出的图形,并自动消除两个淡入淡出片段间的重叠...
unigui_网页iframe交互_完善
+ 交互式代码片段 将交互式代码片段添加到 Remark 幻灯片放映。 有关详细信息,请安装示例幻灯片。 ##安装 npm install npm start 浏览本地主机:8080 ##幻灯片开发 用于实时更新运行 npm run start-dev # # ...
粘贴一片段静态交互的模式 <li class="icon {#icon} left"></li> <li class="info left"><p class="title">{#title}</p><p>{#info}</p></li> .dataModel 启动静态交互 url json路径 {#*}对应json键名。 ...
1.【get方式】使用jquery的get json与后台交互 前端js代码片段 var data= { 'a': $('input[name="a"]').val(), 'b': $('input[name="b"]').val() } $.getJSON($SCRIPT_ROOT + '/_add_numbers',data, function(data)...
代码片段: clickFun:function(){ if(typeof this.bufferImg=="undefined"){ this.bufferImg=this.img; this.img=changeImgColor(this.bufferImg,colorObj.r,colorObj.g,colorObj.b); }else{ this.img=...
voicesignin.com身份验证平台此存储库包含摘要,示例以及对与VoiceSignin API交互的客户端和服务器端代码的贡献。 从此处获取代码段,并从voicesignin.com开发人员门户( )获取您的API密钥。 语音登录是一种创新的...
ShaderWorkshop:使用Qt制作的交互式GLSL片段着色器编辑器
Livy是一个开放源代码的REST接口,用于从任何地方与进行交互。 它支持在本地运行或在中运行的Spark上下文中执行代码片段或程序片段。 交互式Scala,Python和R Shell 使用Scala,Java,Python批量提交 多个用户...
流浪者 交互式浏览DOM结构的小片段 要使用此代码段,只需将其粘贴到浏览器控制台并运行即可。 然后您可以将鼠标光标移到网页上并查看网页结构,如下所示:
DevChat 为开发人员提供了一种非常实用和有效的方式来与大型语言模型 (LLM) 进行交互和协作。AI编码助手通过手动控制生效。没有过度的自动化,恰到好处地使用人工智能。使用简单,无需复杂的提示工程。专为可扩展...
这是交互式登录和注册用户交互(UI)设计。 在Android Studio中运行此代码后,您一定会喜欢它。 有一些完整的UI设计的屏幕截图,并且还上传了UI的一个gif文件,从那里我想到了在Android编程中实现它的想法。 使用...
片段 用户交互 EnableDisableUsingRadioButton.html Description: Based on radio button value (Yes or no) the dropdown will visible/hidden. If placement willingness is yes then placement willingness ...
HIFI是一组工具,可以根据Hi-C数据以限制性片段(RF)分辨率推断真正的染色体内相互作用频率。 它使用自适应核密度估计和马尔可夫随机场方法从稀疏读取计数矩阵中提供对交互频率矩阵的准确估计。 输入:与来自给定...
[live-examples]-包含实时示例CSS和JS片段。 [媒体]-包含实时示例和模板使用的图像。 [tmpl]-基本模板。 动态生成的页面,它们的依存关系和资产将生成到prod分支。浏览器支持基准以下是交互式编辑器支持的浏览器/...
本文以斗鱼直播平台为例,通过分析观众的发弹幕与送礼物行为,提出了基于直播间弹幕数量时间序列和礼物价值时间序列的精彩片段自动化检测方法.首先利用z-score方法检测序列高潮,然后对高潮做样本标注和特征构建,...
Snipget 应用程序的 Eclipse 客户端,以便能够与SaaS Snipget交互: Snipet as a Service 它是一种用于存储和访问在线代码片段的云服务,进行特定研究以建立真正的分布式代码库。 可通过以下 URL 访问服务器: : ...
活动 初始化 v0.1,跨交互的目的地,例如活动、服务、片段或任何其他。
├── _footer.html 页脚公共代码片段 ├── _header.html 头部公共代码片段 ├── _meta.html meta公共代码片段 ├── robots.txt 搜索引擎爬虫配置文件 ├── login.html 管理员登陆 ├── ...