一、总体工程图:

二、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. 教你看懂C++类库函数定义之一---HRESULT 宏

    一切从一个C++ 类库头文件开始,现在在做一个C++的项目,期间用到一个开源的界面库DUILib(类似MFC),这个东西还不错能很容易的写出漂亮的界面,比如QQ的界面,可以去下载下来研究研究,地址:h ...

  2. O2O难解餐饮行业趋势下行之困

    近几年,O2O这个名词越来越常见,我们不但能够在IT相关资讯栏目看到它的存在,甚至在一些综合新闻版面也能轻易看到. 诚然.线下商家结合线上引流这样的方法,能够带来不少订单,可是O2O是否就能够解决餐饮 ...

  3. BZOJ 1592: [Usaco2008 Feb]Making the Grade 路面修整( dp )

    最优的做法最后路面的高度一定是原来某一路面的高度. dp(x, t) = min{ dp(x - 1, k) } + | H[x] - h(t) | ( 1 <= k <= t ) 表示前 ...

  4. BZOJ 2190: [SDOI2008]仪仗队( 欧拉函数 )

    假设C君为(0, 0), 则右上方为(n - 1, n - 1). 一个点(x, y) 能被看到的前提是gcd(x, y) = 1, 所以 answer = ∑ phi(i) * 2 + 2 - 1 ...

  5. Oracle闪回flashback总结

    1.说明: Ø  采用的技术. 使用的是多个技术. 1.      闪回日志 2.      回收站 3.      回滚段 无法使用回收站的操作 Drop table xxx purge; Drop ...

  6. js数组基础整理

    首页: 主要整理了一下数组中常用的一些基础知识,代码都是自己手敲,有不对的地方希望能指出,目前只有4篇,后续会不断的增加这一板块. 由于少于100字不能发所以把一些最基本的创建数组也写上. // 创建 ...

  7. Delphi中JSon SuperObject 使用:数据集与JSON对象互转

    在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包 ...

  8. 一些Windows API导致的Crash以及使用问题总结

    RegQueryValueEx gethostbyname/getaddrinfo _localtime64 FindFirstFile/FindNextFile VerQueryValue Crea ...

  9. Logistic Regression(逻辑回归)(一)基本原理

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 虽然叫做“回归”,但是这个算法是用来解决分类问题的.回归与分类的区 ...

  10. sqlserver存储过程学习笔记(一)基础知识篇(全)

    说出来有点丢人,做sqlserver应用系统近一年,竟然没有使用过存储过程,现在就好好的梳理一下对应知识,慢慢让其加入到我的项目中去吧. 存储过程的优点:1.运行效率高,提供了在服务器端快速执行sql ...