Android應(yīng)用開(kāi)發(fā)中Fragment與Activity間通信示例講解
首先,如果你想在android3.0及以下版本使用fragment,你必須引用android-support-v4.jar這個(gè)包
然后你寫(xiě)的activity不能再繼承自Activity類了,而是要繼承android.support.v4.app.FragmentActivity,一些其他的父類也有相應(yīng)的變化.
由于在android的實(shí)現(xiàn)機(jī)制中fragment和activity會(huì)被分別實(shí)例化為兩個(gè)不相干的對(duì)象,他們之間的聯(lián)系由activity的一個(gè)成員對(duì)象fragmentmanager來(lái)維護(hù).fragment實(shí)例化后會(huì)到activity中的fragmentmanager去注冊(cè)一下,這個(gè)動(dòng)作封裝在fragment對(duì)象的onAttach中,所以你可以在fragment中聲明一些回調(diào)接口,當(dāng)fragment調(diào)用onAttach時(shí),將這些回調(diào)接口實(shí)例化,這樣fragment就能調(diào)用各個(gè)activity的成員函數(shù)了,當(dāng)然activity必須implements這些接口,否則會(huì)包c(diǎn)lasscasterror
fragment和activity的回調(diào)機(jī)制又是OOP的一次完美演繹!
下面通過(guò)一個(gè)例子來(lái)說(shuō)明:
首先,我們看下界面


左邊的TextView會(huì)根據(jù)右邊點(diǎn)擊button的不同而改變。
下面開(kāi)始介紹代碼:
1.在layout里新建fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
可以看出,這里就只有一個(gè)TextView
2.在layout里新建fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 3" /> </LinearLayout>
這里是三個(gè)button
3.創(chuàng)建類Fragment1繼承Fragment
package lgx.fram.framents;
import android.app.Fragment;
import android.os.Bundle;
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);
}
}
重寫(xiě)onCreateView()方法,這里 return inflater.inflate(R.layout.fragment1, container, false); 這句話是重點(diǎn)
4.創(chuàng)建類Fragment2繼承Fragment
package lgx.fram.framents;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
TextView textview;
Button button, button2, button3;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.button);
button2 = (Button) getActivity().findViewById(R.id.button2);
button3 = (Button) getActivity().findViewById(R.id.button3);
textview = (TextView) getActivity().findViewById(R.id.fragment_text);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textview.setText(button.getText());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textview.setText(button2.getText());
}
});
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textview.setText(button3.getText());
}
});
}
}
button = (Button) getActivity().findViewById(R.id.button);
通過(guò)這種方法來(lái)得到fragment上面的控件
5.activity_fragment.xml里面的代碼是這個(gè)樣子的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="lgx.fram.framents.Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="lgx.fram.framents.Fragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
注意:控件fragment里的android:name=" "里面填寫(xiě)的是你的Fragment類的絕對(duì)路徑(腦子突然短路,是這樣說(shuō)的嗎??),id用來(lái)標(biāo)示fragment。
6.FragmentActivity是最簡(jiǎn)單的,就只是setContentView,并沒(méi)有進(jìn)行其他改變??聪旅?/p>
package lgx.fram.framents;
import android.app.Activity;
import android.os.Bundle;
public class FragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
}
}
在這里我的整個(gè)小應(yīng)用就做完了。我這里的Fragment通過(guò)布局文件加入到Activity里的,還有另一種方式是通過(guò)編程的方式將Fragment加入Activity里。這里我簡(jiǎn)單敘述
上面的1,2,3,4都不需要?jiǎng)印?/p>
第5步驟,activity_fragment.xml里面的代碼變成下面的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > </LinearLayout>
你會(huì)發(fā)現(xiàn)我知識(shí)去掉了兩個(gè)Fragment,整個(gè)LinearLayout加進(jìn)去了id
第6個(gè)步驟,里面的注釋,已經(jīng)寫(xiě)得很清楚了:
package lgx.fram.framents; import android.os.Bundle; import android.app.Activity; import android.view.Display; import android.view.Menu;
@author lenovo 動(dòng)態(tài)添加Fragment主要分為4步:
(1)獲取到FragmentManager,在Activity中可以直接通過(guò)getFragmentManager得到。
(2)開(kāi)啟一個(gè)事務(wù),通過(guò)調(diào)用beginTransaction方法開(kāi)啟。
(3)向容器內(nèi)加入Fragment,一般使用replace方法實(shí)現(xiàn),需要傳入容器的id和Fragment的實(shí)例。
(4)提交事務(wù),調(diào)用commit方法提交。
public class FragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
getFragmentManager().beginTransaction()
.replace(R.id.main_layout, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2();
getFragmentManager().beginTransaction()
.replace(R.id.main_layout, fragment2).commit();
}
}
}
這個(gè)代碼的意思是,橫豎屏顯示不同的Fragment。如果是模擬機(jī)測(cè)試,請(qǐng)按Ctrl+F11。
- Android基礎(chǔ)之Fragment與Activity交互詳解
- Android 管理Activity中的fragments
- Android中fragment與activity之間的交互(兩種實(shí)現(xiàn)方式)
- Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解
- Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器
- Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
- Android Fragment與Activity之間的相互通信實(shí)例代碼
- 詳解Android activity與fragment之間的通信交互
- Android 中Fragment與Activity通訊的詳解
- android橫豎屏切換時(shí)候Activity的生命周期
- Android Activity 橫豎屏切換的生命周期
- Android開(kāi)發(fā)使用Activity嵌套多個(gè)Fragment實(shí)現(xiàn)橫豎屏切換功能的方法
相關(guān)文章
android ContentResolver獲取手機(jī)電話號(hào)碼和短信內(nèi)容
這篇文章主要為大家詳細(xì)介紹了android ContentResolver獲取手機(jī)電話號(hào)碼、短信內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache
本篇文章主要介紹了淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
android TextView中識(shí)別多個(gè)url并分別點(diǎn)擊跳轉(zhuǎn)方法詳解
在本篇文章里小編給大家整理的是關(guān)于android TextView中識(shí)別多個(gè)url并分別點(diǎn)擊跳轉(zhuǎn)方法詳解,需要的朋友們可以學(xué)習(xí)參考下。2019-08-08
Android 為L(zhǎng)istView添加分段標(biāo)頭的方法
下面小編就為大家?guī)?lái)一篇Android 為L(zhǎng)istView添加分段標(biāo)頭的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android Handler之消息循環(huán)的深入解析
本篇文章是對(duì)Handler消息循環(huán)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android?模擬地圖定位功能的實(shí)現(xiàn)
這篇文章主要介紹了Android?模擬地圖定位功能的實(shí)現(xiàn),本工程利用手機(jī)自帶的"模擬位置"功能實(shí)現(xiàn)運(yùn)行時(shí)修改LocationManager結(jié)果,需要的朋友可以參考一下2022-02-02

