動態(tài)創(chuàng)建fragment的流程
1.0 新建一個類繼承fragment.
2.0 在自定義的fragment里面復(fù)寫oncreateVIew的方法
3.0 在onCreateVIew的方法里使用inflate填充器
4.0 通過Return方法把inflate得到View對象給返回出去
5.0 在使用fragment的activity里面調(diào)用getFragmentManager方法.得到fragmentManager對象
6.0 通過fragment管理對象,開啟事務(wù)
7.0 使用事務(wù)對象,調(diào)用replace方法,替換fragment,是動態(tài)使用fragment精華
8.0 使用事務(wù)對象進(jìn)行提交.
動態(tài)創(chuàng)建fragment的流程可以兼容低版本的安卓系統(tǒng)
1.0 導(dǎo)入包一律都是V4包下的
2.0 關(guān)于你們要使用到fragment的activity,一定要繼承fragmentActivity
3.0 在或者fragment管理對象時,你們使用方法是getSupportFragmentManager靜態(tài)方法.
fragment是activity的一部分,他依賴于Activity
fragment依賴于activity,不能單獨(dú)存在,fragment的生命周期收到activity的生命周期的影響.
第一步,new class 繼承 Fragment.
第二步,復(fù)寫onCreateView的方法
第三步,在onCreateView方法里面進(jìn)行,使用inflater把layout布局文件轉(zhuǎn)換為一個View對象
第四步,在onCreateView的return方法里,把我們的View對象返回出去
第五步,在要使用activity的布局里面,像使用控件的方式把我們的fragment定義到ViewGroup(就是布局里面)
動態(tài)使用fragment的步驟:
第一步,new class 繼承 Fragment
第二步,復(fù)寫onCreateView方法
第三步,在onCreateView里面進(jìn)行,使用inflater把layout布局文件轉(zhuǎn)換為一個View對象
第四步.在onCreateVIew的return方法里,把我們的View對象返回出去
第五步.在java代碼里通過靜態(tài)方法getFragmentManager獲取fragmentManager管理
第六步,通過fragmentManager的beginTransaction得到事務(wù)對象
第七步,通過事務(wù)對象調(diào)用.replace方法,替換控件為fragment
第八步,使用事務(wù)對象提交commit
v4兼容包下的fragment使用(現(xiàn)在開發(fā)基本不用了)
1.0 自定義fragment類里繼承v4包下的fragment.記住所有用到fragment地方導(dǎo)入包必須一致
2.0 你們自定義的activity必須繼承FragmentActivity
3.0 獲取FragmentManager對象時,必須用getSupportFragmentManager方法.而不是getFragmentManager.
下面是我做的一個小Demo
是在一個頁面中實(shí)現(xiàn)各個Activity之間的通信,左側(cè)點(diǎn)擊按鈕,右側(cè)出現(xiàn)相應(yīng)的Activity界面.同時on關(guān)實(shí)現(xiàn)兩個Activity之間的通信.
第一步,在布局文件main_Activity中設(shè)置按鈕button和文本.然后加上布局文件FrameLayout.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/Btton_a1"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a1"/>
android:id="@+id/Btton_a2"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a2"/>
android:id="@+id/Btton_a3"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a3"/>
android:id="@+id/Btton_a4"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a4"/>
android:id="@+id/Btton_a5"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a5"/>
android:id="@+id/Btton_a6"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a6"/>
android:id="@+id/Btton_a7"
android:background="@drawable/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a7"/>
android:id="@+id/Activity_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity傳來的數(shù)據(jù)"/>
android:id="@+id/Activity_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callMe"/>
android:id="@+id/Activity_callChild"
android:layout_width="wrap_content"
android:background="@drawable/bg"
android:layout_height="wrap_content"
android:text="@string/a8"/>
android:id="@+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
在MainActivity中編寫代碼.
先編寫一個方法initview()實(shí)現(xiàn)初始化.
然后根據(jù)swich開始編寫Fragment.這里有七個Fragment,每個Fragment里面代碼內(nèi)容相似就用一個做代表.
這里Fragment1的Activity代碼最為復(fù)雜.
private voidinitview() {
Btton_a1= (Button) findViewById(R.id.Btton_a1);
Btton_a2= (Button) findViewById(R.id.Btton_a2);
Btton_a3= (Button) findViewById(R.id.Btton_a3);
Btton_a4= (Button) findViewById(R.id.Btton_a4);
Btton_a5= (Button) findViewById(R.id.Btton_a5);
Btton_a6= (Button) findViewById(R.id.Btton_a6);
Btton_a7= (Button) findViewById(R.id.Btton_a7);
Activity_tv= (TextView) findViewById(R.id.Activity_tv);
Activity_et= (EditText) findViewById(R.id.Activity_et);
Activity_callChild= (Button) findViewById(R.id.Activity_callChild);
temp= (FrameLayout) findViewById(R.id.temp);
Btton_a1.setOnClickListener(this);
Btton_a2.setOnClickListener(this);
Btton_a3.setOnClickListener(this);
Btton_a4.setOnClickListener(this);
Btton_a5.setOnClickListener(this);
Btton_a6.setOnClickListener(this);
Btton_a7.setOnClickListener(this);
Activity_callChild.setOnClickListener(this);
}
@Override
public voidonClick(View v) {
fragmentManager=this.getFragmentManager();
FragmentTransaction beginTransaction =fragmentManager.beginTransaction();
switch(v.getId()) {
caseR.id.Btton_a1:
extracted();
break;
caseR.id.Btton_a2:
fragment2 fragement2 =newfragment2();
beginTransaction.replace(R.id.temp, fragement2);
break;
caseR.id.Btton_a3:
fragment3 fragment3 =newfragment3();
beginTransaction.replace(R.id.temp, fragment3);
break;
caseR.id.Btton_a4:
fragment4 fragment4 =newfragment4();
beginTransaction.replace(R.id.temp, fragment4);
break;
caseR.id.Btton_a5:
fragment5 fragment5 =newfragment5();
beginTransaction.replace(R.id.temp, fragment5);
break;
caseR.id.Btton_a6:
fragment6 fragment6 =newfragment6();
beginTransaction.replace(R.id.temp, fragment6);
break;
caseR.id.Btton_a7:
fragment7 fragment7 =newfragment7();
beginTransaction.replace(R.id.temp, fragment7);
break;
caseR.id.Activity_callChild:
break;
}
beginTransaction.commit();
}
Fragment1的布局代碼和Activity代碼:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/a2"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3C6"
android:text="
佇倚危樓風(fēng)細(xì)細(xì)阴挣,望極春愁掸宛,
黯黯生天際轴合。
草色煙光殘照里静尼,無言誰會憑欄意沟绪。
擬把疏狂圖一醉欧啤,對酒當(dāng)歌脯爪,
強(qiáng)樂還無味。
衣帶漸寬終不悔就漾,為伊消得人憔悴呐能。"/>
android:id="@+id/fragment_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity傳來的數(shù)據(jù)"/>
android:id="@+id/fragment_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callYou"/>
android:id="@+id/fragment_callChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點(diǎn)擊事件"/>
這里的Activity中加入了通信,要仔細(xì)看
packagefragment.com.hulufragment;
importandroid.app.Activity;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.text.TextUtils;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
importandroid.widget.Toast;
/**
* Created by Administrator on 2016/10/1.
*/
public classfragment1extendsFragment {
privateActivityhomeActivity;
privateEditTextactivity_et;
privateViewview;
privateTextViewfragment_tv;
privateEditTextfragment_et;
privateTextViewactivity_tv;
@Nullable
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment1,null);
homeActivity= getActivity();
ActivityInit();
fragemtnInit();
returnview;
}
private voidfragemtnInit() {
fragment_tv= (TextView)view.findViewById(R.id.fragment_tv);
fragment_et= (EditText)view.findViewById(R.id.fragment_et);
activity_tv= (TextView)homeActivity.findViewById(R.id.Activity_tv);
Button fragment_callChild = (Button)view.findViewById(R.id.fragment_callChild);
fragment_callChild.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
String trim =fragment_et.getText().toString().trim();
if(TextUtils.isEmpty(trim)){
Toast.makeText(homeActivity,"不能為空",Toast.LENGTH_LONG).show();
return;
}
activity_tv.setText(trim);
}
});
}
private voidActivityInit() {
Button Activity_callChild =(Button)homeActivity.findViewById(R.id.Activity_callChild);
activity_et= (EditText)homeActivity.findViewById(R.id.Activity_et);
Activity_callChild.setOnClickListener(newView.OnClickListener() {
public voidonClick(View v) {
String trim =activity_et.getText().toString().trim();
if(TextUtils.isEmpty(trim)){
Toast.makeText(homeActivity,"不能為空",Toast.LENGTH_LONG).show();
return;
}
fragment_tv.setText(trim);
}
});
}
}
其他的Fragment布局文件和Activity都相似:
packagefragment.com.hulufragment;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.TextView;
/**
* Created by Administrator on 2016/10/1.
*/
public classfragment2extendsFragment {
privateTextViewtv_temp;
@Nullable
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2,null);
//fragment的布局控件的查找,就要用到inflater得到的VIew對象.
returnview;
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/a2"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#3C6"
android:text="
佇倚危樓風(fēng)細(xì)細(xì),望極春愁抑堡,
黯黯生天際摆出。
草色煙光殘照里,無言誰會憑欄意首妖。
擬把疏狂圖一醉偎漫,對酒當(dāng)歌,
強(qiáng)樂還無味有缆。
衣帶漸寬終不悔象踊,為伊消得人憔悴。"/>
android:id="@+id/fragment_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity傳來的數(shù)據(jù)"/>
android:id="@+id/fragment_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="callYou"/>
android:id="@+id/fragment_callChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點(diǎn)擊事件"/>
這是我代碼的地址,僅供參考.https://github.com/ZoeSj/FourFragment/tree/master/hulufragment