本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
通常情况下,一个activity可能包含一个或多个fragment,它们协同工作,组成一个连贯的UI界面。在这种情况下,多个fragments之间的通信显得就很重要了。举个例子,一个activity包含左右两个fragment,左侧的fragment包含了一个列表(比如新闻题目列表),当点击每个新闻题目的时候,右侧的fragment就会显示这条新闻的详尽信息。
下面展示如何进行操作。
Fragment1在整个activity的左侧,Fragment2在右侧。
1. fragment1.xml中的代码。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00FF00"
- android:orientation="vertical">
- <TextView
- android:id="@+id/lblFragment1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Thisisfragment#1"
- android:textColor="#000000"
- android:textSize="25sp"/>
- </LinearLayout>
2. fragment2.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFE00"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Thisisfragment#2"
- android:textColor="#000000"
- android:textSize="25sp"/>
- <Button
- android:id="@+id/btnGetText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="onClick"
- android:text="GettextinFragment#1"
- android:textColor="#000000"/>
- </LinearLayout>
3. main.xml中的代码。
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <fragment
- android:id="@+id/fragment1"
- android:name="net.learn2develop.Fragments.Fragment1"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="1"/>
- <fragment
- android:id="@+id/fragment2"
- android:name="net.learn2develop.Fragments.Fragment2"
- android:layout_width="0px"
- android:layout_height="match_parent"
- android:layout_weight="1"/>
- </LinearLayout>
4. FragmentsActivity.java中的代码。
- publicclassFragmentsActivityextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- publicvoidonClick(Viewv){
- TextViewlbl=(TextView)findViewById(R.id.lblFragment1);
- Toast.makeText(this,lbl.getText(),Toast.LENGTH_SHORT).show();
- }
- }
5. Fragment2.java中的代码。
- publicclassFragment2extendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- returninflater.inflate(R.layout.fragment2,container,false);
- }
- @Override
- publicvoidonStart(){
- super.onStart();
- ButtonbtnGetText=(Button)getActivity()
- .findViewById(R.id.btnGetText);
- btnGetText.setOnClickListener(newView.OnClickListener(){
- publicvoidonClick(Viewv){
- TextViewlbl=(TextView)getActivity().findViewById(
- R.id.lblFragment1);
- Toast.makeText(getActivity(),lbl.getText(),Toast.LENGTH_SHORT)
- .show();
- }
- });
- }
- }
6. 调试。
点击右边的“Get text in Fragment #1”按钮,将弹出一个提示。
分享到:
相关推荐
[Packt Publishing] 动态 UI 开发教程 Android Fragments 实现 英文版 [Packt Publishing] Creating Dynamic UI with Android Fragments E Book ☆ 图书概要:☆ Leverage the power of Android fragments to ...
Android Fragments, 2014, pdf
android-tutorial, 在android中,特性动画和 Fragments 教程 Android教程这里项目包含两个部分。 属性动画与 fragment 在Android中的应用。属性动画一个简单而完整的演示,可以在安卓中实现。 获取更多信息在 ...
This book looks at the impact fragments have on Android UI design and their role in both simplifying many common UI challenges and in providing best practices for incorporating rich UI behaviors....
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult at best. As developers, we need more control than that afforded by activities. We need a new ...
Android: ViewPager and Fragments The FragmentPagerActivity class creates the ViewPager and the associated FragmentPagerAdapter.
NULL 博文链接:https://chenchuangfeng.iteye.com/blog/1881059
代码来自博客实例:http://blog.csdn.net/lmj623565791/article/details/42628537 有问题请博客留言~~
Creating Dynamic UI with Android Fragments Leverage the power of Android Fragments to develop dynamic user interfaces for your apps
fragments
#Training:片段 + MVC 该项目由一个Activity组成,该Activity具有关于一个人的基本信息表。 更新“名称”字段时,屏幕标题会更新为问候语。 该项目结构MainActivity :所有责任都在MainActivity 。...
AndroidAutowire, Android视图activity 和 Fragments的Java注释方法 Android自动装配使用Java注释和反射,这个库将允许你用基于注释的方法替换来自 activity 。Fragments 和视图的一些烦人的样板设置。这里知识库在...
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult. As developers, we need more control than that afforded by activities. We need a new approach: ...
Trying to meet these demands using Android's traditional activity-centric UI design model is dif cult at best. As developers, we need more control than that afforded by activities. We need a new ...
基于Android框架的动态UI构建(Creating Dynamic UI with Android Fragments, 2nd Edition)-2016英文原版,0积分——该书是2016年最新的第2版,全书154页。
fragements例子 博文链接:https://dasheng.iteye.com/blog/986809
If you are an Android developer then you know the pain of an Android Application birth, the logs, the screens, the lists, view pagers, recyclers, adapters, activities, fragments, services, intents and...
nested fragments nested fragments nested fragments nested fragments
Depth:给你的fragments添加一些Depth
使用Fragment如何创建高性能UI;使用Fragment如何创建高性能UI