一、总体工程图:

二、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. java 随机数的生成

    生成10个不小于100000的6位数 public static void main(String[] args) { Random random = new Random(); for (int i ...

  2. android的JNI标准 android的NDK

    转载的! Java Native Interface (JNI)标准是java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 Java 虚拟机 (VM) ...

  3. Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间

    Error: 'The service did not respond in a timely fashion' (ServicesPipeTimeout) when attempting when ...

  4. No resource found that matches the given name 'android:WindowTitle'

    当你的androidAPI 由2.1版本更换成2.2版本时:  res/vavlues/styles.xml中使用的android:WindowTitle会报以下异常, error: Error re ...

  5. C++ 库研究笔记——通过inline避免hpp 的mutiple definition 错误

    C++用了这么多年,这个却第一次知道,以前没用过hpp 这样: // foo.hpp void foo() { /* body */ } // a.cpp #include "foo.hpp ...

  6. HDU 3468 BFS+二分匹配

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...

  7. 浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC

    整理-----数据库技术,磁盘冗余阵列,IP分配, ECC内存,ADO, DAO,JDBC 1.MySQL MySQL是最受欢迎的开源SQL数据库管理系统,它由 MySQL AB开发.发布和支持.My ...

  8. 菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t

    菜鸟nginx源代码剖析数据结构篇(一)动态数组ngx_array_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...

  9. 微软推荐的130道ASP.NET常见面试题及答案

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  10. Flash Builder 条件编译的实现

    最近项目需要开发多个版本,  而Flash又没有像C++ 那样的 #ifdef,  来让一套代码支持多个版本的编译发布; 经过研究, 终于知道Flash Builder如何支持条件编译: 1. 在项目 ...