Fragment总结
一、总体工程图:
二、main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" > <fragment
android:id="@+id/fragment1"
android:name="com.jltxgcy.fragmentdemo.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_weight="1" > </LinearLayout>
<TextView
android:id="@+id/tv_display"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </LinearLayout>
三、fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >
<TextView
android:id="@+id/tv_fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 1"
android:textColor="#000000"
android:textSize="25sp" /> </LinearLayout>
四、fragment2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00" > <TextView
android:id="@+id/tv_fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is fragment 2"
android:textColor="#000000"
android:textSize="25sp" /> </LinearLayout>
五、MainActivity.java
package com.jltxgcy.fragmentdemo; import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu; public class MainActivity extends FragmentActivity {
public static final String TAG = "Fragment2"; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Fragment2 fragment2 = new Fragment2();
getSupportFragmentManager().beginTransaction().replace(R.id.main_layout, fragment2).commit();
} public void testMainActivity(){
Fragment1 fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment1.testFragment1();
} public void testFindviewById(){
findViewById(R.id.tv_display);
findViewById(R.id.tv_fragment2);
}
}
六、Fragment1.java
package com.jltxgcy.fragmentdemo; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
} public void testFragment1(){
Log.d("jltxgcy", "testFragment1");
}
}
七、Fragment2.java
package com.jltxgcy.fragmentdemo; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment2 extends Fragment {
private ViewGroup mViewGroup; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mViewGroup = container;
testFragment2();
return inflater.inflate(R.layout.fragment2, container, false);
} public void testFragment2(){
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.testMainActivity();
Fragment1 fragment1 = (Fragment1) getFragmentManager().findFragmentById(R.id.fragment1);
fragment1.testFragment1();
} public void testFindviewById(){
mViewGroup.findViewById(R.id.tv_fragment2);
getActivity().findViewById(R.id.tv_display);
getActivity().findViewById(R.id.tv_fragment1);
} }
八、上面分别介绍了动态和静态加载Fragment的过程。
Activity中获取自身控件View:findViewById(R.id.tv_display);
Activity中获取Fragment控件View:findViewById(R.id.tv_fragment2);
Activity中使用Fragment方法:
Fragment1 fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
fragment1.testFragment1();
Fragment中获取自身控件View:mViewGroup.findViewById(R.id.tv_fragment2);
Fragment中获取Activity控件View:getActivity().findViewById(R.id.tv_display);
Fragment中获取其他控件Fragment的View:getActivity().findViewById(R.id.tv_fragment1);
Fragment中使用Activity中的方法:
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.testMainActivity();
Fragment中使用其他Fragment中的方法:
Fragment1 fragment1 = (Fragment1) getFragmentManager().findFragmentById(R.id.fragment1);
fragment1.testFragment1();
九、程序运行最后结果为:
十、代码待后我会传到github上。
Fragment总结的更多相关文章
- 浅谈 Fragment 生命周期
版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...
- 札记:Fragment基础
Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...
- EventBus实现activity跟fragment交互数据
最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...
- Android:Activity+Fragment及它们之间的数据交换.
Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...
- Android中Fragment和ViewPager那点事儿(仿微信APP)
在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...
- Android开发学习—— Fragment
#Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中Fragment的两种创建方式
fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...
- Android Fragment 剖析
1.Fragment如何产生?2.什么是Fragment Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
随机推荐
- load data 方式导入的数据不可以用binlog日志进行恢复,因为binlog里面不产生insert sql语句。
QQ群里面有人问起这个问题: 用load data 导入数据的时候,在binlog文件中记录的不是insert 语句,这样的话,如果用load data 导入数据,当需要恢复数据库的时候 bi ...
- 为开发用途mac电脑瘦身
本文介绍的瘦身方法仅仅针对开发用途的mac电脑,在我们的开发过程中会发现随着编译的程序过多非常导致硬盘空间的占用率不断上升. 原文地址:http://blog.csdn.net/qqmcy/artic ...
- NET 2016
.NET 2016 阅读目录 初识 .NET 2016 使用 .NET Framework 4.6 编译应用程序 使用 .NET Core CLI 编译应用程序 小结 厚积薄发这个词是高三英语老师 ...
- perl 当前包会覆盖父类的同名方法
12.5.2 访问被覆盖的方法: 当一个类定义一个方法,那么该子过程覆盖任意基类中同名的方法. [root@wx03 test]# cat Horse.pm package Horse; our @I ...
- 基于visual Studio2013解决C语言竞赛题之1027 YN
题目 解决代码及点评 /* 计算Yn的值,直到|Yn - Yn-1|<10-6为止,并打印出此时共作了多少次COS计算. 提示:Yn+1=COS(Yn),故本 ...
- <Win32_12>程序员求爱的创意程序——升级版^_^
前段时间,我编写了一个创意程序,并用于向自己目前的女朋友表白,结果效果还不错,得到了她的芳心. 于是我将自己的创意程序共享到csdn资源上,大多数网友认为创意不错,就是简单了些——呵呵,其实我个人也这 ...
- SqlParameter参数化查询
上篇博客写了关于重构代码用到的SQLHelper类,这个类包括四种函数,根据是否含参和是否有返回值各分两种.在这里写写传参过程用到的SqlParameter. 如果我们使用如下拼接sql字符串的方式进 ...
- 关于android多点触控
最近项目需要一个多点触控缩放的功能.然后上网查了下资料 总结一下: 首先android sdk版本很重要,比如你在AndroidManifest.xml中指定android:minSdkVersion ...
- Windows Service的安装卸载 和 Service控制
原文 Windows Service的安装卸载 和 Service控制 本文内容包括如何通过C#代码安装Windows Service(exe文件,并非打包后的安装文件).判断Service是否存在. ...
- 1.0.3-学习Opencv与MFC混合编程之---打开本地摄像头
源代码:http://download.csdn.net/detail/nuptboyzhb/3961643 版本1.0.3新增内容 打开摄像头 Ø 新建菜单项,Learning OpenCV——&g ...