一、总体工程图:

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 {@Overridepublic 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;@Overridepublic 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上。

更多相关文章

  1. 详解Android读取本地图片和网络图片的方法
  2. android onSaveInstanceState的使用方法
  3. android —— 自定义控件 竖向的ViewPager,上下滑动的组件,android
  4. 【特效】【自定义控件】关键字飘飞效果
  5. android获取sd卡路径方法:
  6. 日期选择控件
  7. android中常见的二种数据解析方法----XML和Json
  8. Android SDK Manager无法更新解决方法
  9. Android Timer 更好方法

随机推荐

  1. Android(安卓)ScrollView去掉滚动条及Scr
  2. Android(安卓)TextView文字横向自动滚动(
  3. Android加载Gif和ImageView的通用解决方
  4. Android(安卓)NDK
  5. Android内存分析和调优
  6. Android的MediaPlayer架构介绍
  7. android聊天列表实现
  8. android JNI 学习笔记
  9. Android学习笔记---第二天---基础UI组件-
  10. Android_UI开发专题