一、总体工程图:

二、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总结的更多相关文章

  1. 浅谈 Fragment 生命周期

    版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...

  2. 札记:Fragment基础

    Fragment概述 在Fragment出现之前,Activity是app中界面的基本组成单位,值得一提的是,作为四大组件之一,它是需要"注册"的.组件的特性使得一个Activit ...

  3. EventBus实现activity跟fragment交互数据

    最近老是听到技术群里面有人提出需求,activity跟fragment交互数据,或者从一个activity跳转到另外一个activity的fragment,所以我给大家介绍一个开源项目,EventBu ...

  4. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  5. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  6. Android开发学习—— Fragment

    #Fragment* 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容* 生命周期方法跟Activity一致,可以理解把其为就是一个Activity* 定义布局文件作 ...

  7. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  8. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  9. Android Fragment 剖析

    1.Fragment如何产生?2.什么是Fragment Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. Bar Codes

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/N #include<map> #include&l ...

  2. C/C++迭代器使用具体解释

    迭代器是一种检查容器内元素并遍历元素的数据类型.能够替代下标訪问vector对象的元素. 每种容器类型都定义了自己的迭代器类型,如 vector: vector<int>::iterato ...

  3. 高仿114la网址导航源码完整最新版

    给大家本人我精心模仿的高仿114la网址导航源码,我们都知道114la网址导航的影响力,喜欢的朋友可以下载学习一下.  由于文件较大,没有上传了,下载地址在下面有的. 附源码下载: 114la网站导航 ...

  4. WPF(布局)

      WPF编程学习——布局   本文目录 1.布局简介 2.面板(Panel) 3.视图框(Viewbox) 4.滚动视图控件(ScrollViewer) 5.公共布局属性 1.布局简介 应用程序界面 ...

  5. Java进阶02 异常处理

    链接地址:http://www.cnblogs.com/vamei/archive/2013/04/09/3000894.html 作者:Vamei 出处:http://www.cnblogs.com ...

  6. Ajax技术——带进度条的文件上传

    1.概述 在实际的Web应该开发或网站开发过程中,经常需要实现文件上传的功能.在文件上传过程中,经常需要用户进行长时间的等待,为了让用户及时了解上传进度,可以在上传文件的同时,显示文件的上传进度条.运 ...

  7. 如何使用springmvc的@requestbody 返回json数据

    先配置 XXX_ servletxml <!-- 整合jackson 返回一个json格式 --><bean class="org.springframework.web. ...

  8. 【笔记】《通俗详细地讲解什么是P和NP问题》的概念记录

    1问题规模: 要计算或解决一个问题,该问题通常有一个大小规模,用n表示. 2算法的时间复杂度 计算次数与n的关系函数.(因为计算次数隐含时间). 3多项式时间复杂度 所有形如a*n^k+b*n^(k- ...

  9. __sleep和__wakeup

    魔术方法__sleep和__wakeup 串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. 然后再反串行化还原为原来的数据. ...

  10. ubunut在系统恢复模式下无法改动rootpassword的分析和解决

    前些日子本猫的ubuntu 14.10貌似出了点问题,想改动下rootpassword,可是无奈原系统有错正常情况下无法改动啊.这是逼我重装的节奏吗? 在ubuntu开机后马上按住left_shift ...