Android学习笔记(十六)——碎片之间进行交互(附源码)
2014-05-12 21:42
621 查看
碎片之间进行交互
点击下载源码
很多时候,一个活动中包含一个或者多个碎片,它们彼此协作,向用户展示一个一致的UI。在这种情况下,碎片之间能进行通信并交换数据十分重要。
1、使用上一篇中创建的同一个项目,在fragment.xml中添加TextView的标识id:
android:id="@+id/lblFragment1"
2、在fragment2.xml中添加一个Button,用于与fragment1进行交互:
<Button
android:id="@+id/btnGetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get text in Fragment #1"
android:textColor="#000000" />
3、将两个碎片重新添加到main.xml中:
<fragment
android:id="@+id/fragment1"
android:name="net.zenail.Fragments.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="net.zenail.Fragments.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
4、在FragmentsActivity.java中,注释掉上一篇中添加的代码,修改后如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
//
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
//
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(android.R.id.content, fragment1);
} else {
//
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(android.R.id.content, fragment2);
}
fragmentTransaction.commit();
*/
}
5、在Fragment2.java中添加如下代码,实现与Fragment1的交互:
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Button btnGetText = (Button) getActivity()
.findViewById(R.id.btnGetText);
btnGetText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView lbl = (TextView) getActivity().findViewById(
R.id.lblFragment1);//通过getActivity()方法获得当前嵌入了该碎片的活动,再使用findViewById()定位该碎片中包含的视图
Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)
.show();
}
});
}
6、按F11调试应用程序,在右侧的第二个碎片中单击按钮,可以看到弹出一个消息框,内容正是碎片1中TextView的内容,说明获取成功~
点击下载源码
很多时候,一个活动中包含一个或者多个碎片,它们彼此协作,向用户展示一个一致的UI。在这种情况下,碎片之间能进行通信并交换数据十分重要。
1、使用上一篇中创建的同一个项目,在fragment.xml中添加TextView的标识id:
android:id="@+id/lblFragment1"
2、在fragment2.xml中添加一个Button,用于与fragment1进行交互:
<Button
android:id="@+id/btnGetText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get text in Fragment #1"
android:textColor="#000000" />
3、将两个碎片重新添加到main.xml中:
<fragment
android:id="@+id/fragment1"
android:name="net.zenail.Fragments.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="net.zenail.Fragments.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
4、在FragmentsActivity.java中,注释掉上一篇中添加的代码,修改后如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
//
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
//
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(android.R.id.content, fragment1);
} else {
//
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(android.R.id.content, fragment2);
}
fragmentTransaction.commit();
*/
}
5、在Fragment2.java中添加如下代码,实现与Fragment1的交互:
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Button btnGetText = (Button) getActivity()
.findViewById(R.id.btnGetText);
btnGetText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView lbl = (TextView) getActivity().findViewById(
R.id.lblFragment1);//通过getActivity()方法获得当前嵌入了该碎片的活动,再使用findViewById()定位该碎片中包含的视图
Toast.makeText(getActivity(), lbl.getText(), Toast.LENGTH_SHORT)
.show();
}
});
}
6、按F11调试应用程序,在右侧的第二个碎片中单击按钮,可以看到弹出一个消息框,内容正是碎片1中TextView的内容,说明获取成功~
相关文章推荐
- C/C++与Lua之间进行数据函数交互
- js和servlet之间用json的数据格式进行交互
- 【COCOS2DX-LUA 脚本开发之十一】C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API (attempt t
- SpringBoot 集成 rabbitmq 简单实现通过队列进行,订单系统与库存系统,物流系统之间的信息交互
- iOS-在Xcode中使用Git进行源码版本控制(三:版本之间的比较)
- lua教程十一----C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API (attempt t
- android-Allowing Other Apps to Start Your Activity(App之间进行交互)
- (Siverlight应用程序之间)通过宿主(B屏)New对象,进行的交互消息(反射)
- SilverLight浏览器交互之:使用SilverLight程序把外部Html页作为资源进行绘图(附源码)
- 【Android】53、碎片和活动之间进行通信
- 混合开发之webView加载html,android 和 html之间进行数据交互
- 碎片和活动之间进行通信
- Android学习笔记(十五)——碎片的生命周期(附源码)
- C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API (attempt t
- 碎片和活动之间进行通信、碎片之间进行通信
- 客户端和应用服务器之间利用HttpClient和Servlet交互JSON对象进行通信
- 多线程之间如何进行信息交互
- 跨web之间交互-jsp中获取request进行转发
- 利用广播进行两个Activity之间的交互
- 怎么让主机与Hyper-v虚拟机之间进行文件交互