動態(tài)創(chuàng)建Fragment

動態(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末棚壁,一起剝皮案震驚了整個濱河市杯矩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌袖外,老刑警劉巖史隆,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異曼验,居然都是意外死亡泌射,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門鬓照,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熔酷,“玉大人,你說我怎么就攤上這事颖杏〈吭桑” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長翼抠。 經(jīng)常有香客問我咙轩,道長,這世上最難降的妖魔是什么阴颖? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任活喊,我火速辦了婚禮,結(jié)果婚禮上量愧,老公的妹妹穿的比我還像新娘钾菊。我一直安慰自己,他們只是感情好偎肃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布煞烫。 她就那樣靜靜地躺著,像睡著了一般累颂。 火紅的嫁衣襯著肌膚如雪滞详。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天紊馏,我揣著相機(jī)與錄音料饥,去河邊找鬼。 笑死朱监,一個胖子當(dāng)著我的面吹牛岸啡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赫编,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼巡蘸,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了沛慢?” 一聲冷哼從身側(cè)響起赡若,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎团甲,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體黍聂,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡躺苦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了产还。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片匹厘。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖脐区,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤医舆,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站酌泰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匕累。R本人自食惡果不足惜陵刹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望欢嘿。 院中可真熱鬧衰琐,春花似錦、人聲如沸炼蹦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掐隐。三九已至狗热,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瑟枫,已是汗流浹背斗搞。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慷妙,地道東北人僻焚。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像膝擂,于是被迫代替她去往敵國和親虑啤。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容