【Android(安卓)开发教程】Fragments间的交互
16lz
2021-01-24
本章节翻译自《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中的代码。
[java] view plain copy- <?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>
[java] view plain copy
- <?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>
- <?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>
- publicclassFragmentsActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @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();
- }
- }
- publicclassFragment2extendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- //---Inflatethelayoutforthisfragment---
- returninflater.inflate(R.layout.fragment2,container,false);
- }
- @Override
- publicvoidonStart(){
- super.onStart();
- //---Buttonview---
- 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();
- }
- });
- }
- }
点击右边的“Get text in Fragment #1”按钮,将弹出一个提示。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- RemoteviewsService
- 打电话的应用程序源码以及Android基础-----Android生命周期
- Android封装类似微信的顶部TitleBar弹出的PopupWindow代码
- Android多媒体学习十五:使用在内置的Camera录制视频
- [android Studio] unhandled exception: android.os.RemoteExcep
- WebRTC Android(安卓)源码编译
- rk3326 android 8.1 上传sftp时候jsch类报错
- Android(安卓)集成 Zxing 条码扫描器