最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Fragment使用全解

 更新時(shí)間:2021年04月08日 09:12:41   作者:guolin  
這篇文章主要介紹了Android Fragment使用的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下

我們都知道,Android上的界面展示都是通過(guò)Activity實(shí)現(xiàn)的,Activity實(shí)在是太常用了,我相信大家都已經(jīng)非常熟悉了,這里就不再贅述。

但是Activity也有它的局限性,同樣的界面在手機(jī)上顯示可能很好看,在平板上就未必了,因?yàn)槠桨宓钠聊环浅4螅謾C(jī)的界面放在平板上可能會(huì)有過(guò)分被拉長(zhǎng)、控件間距過(guò)大等情況。這個(gè)時(shí)候更好的體驗(yàn)效果是在Activity中嵌入"小Activity",然后每個(gè)"小Activity"又可以擁有自己的布局。因此,我們今天的主角Fragment登場(chǎng)了。

Fragment初探

為了讓界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常類(lèi)似于Activity,可以像Activity一樣包含布局。Fragment通常是嵌套在Activity中使用的,現(xiàn)在想象這種場(chǎng)景:有兩個(gè)Fragment,F(xiàn)ragment 1包含了一個(gè)ListView,每行顯示一本書(shū)的標(biāo)題。Fragment 2包含了TextView和ImageView,來(lái)顯示書(shū)的詳細(xì)內(nèi)容和圖片。

如果現(xiàn)在程序運(yùn)行豎屏模式的平板或手機(jī)上,F(xiàn)ragment 1可能嵌入在一個(gè)Activity中,而Fragment 2可能嵌入在另一個(gè)Activity中,如下圖所示:

而如果現(xiàn)在程序運(yùn)行在橫屏模式的平板上,兩個(gè)Fragment就可以嵌入在同一個(gè)Activity中了,如下圖所示:

由此可以看出,使用Fragment可以讓我們更加充分地利用平板的屏幕空間,下面我們一起來(lái)探究下如何使用Fragment。

首先需要注意,F(xiàn)ragment是在3.0版本引入的,如果你使用的是3.0之前的系統(tǒng),需要先導(dǎo)入android-support-v4的jar包才能使用Fragment功能。

新建一個(gè)項(xiàng)目叫做Fragments,然后在layout文件夾下新建一個(gè)名為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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fragment 1"
        android:textColor="#000000"
        android:textSize="25sp" />
 
</LinearLayout>

可以看到,這個(gè)布局文件非常簡(jiǎn)單,只有一個(gè)LinearLayout,里面加入了一個(gè)TextView。我們?nèi)绶ㄅ谥圃傩陆ㄒ粋€(gè)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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fragment 2"
        android:textColor="#000000"
        android:textSize="25sp" />
 
</LinearLayout>

然后新建一個(gè)類(lèi)Fragment1,這個(gè)類(lèi)是繼承自Fragment的:

public class Fragment1 extends Fragment {
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, container, false);
	}
 
}

我們可以看到,這個(gè)類(lèi)也非常簡(jiǎn)單,主要就是加載了我們剛剛寫(xiě)好的fragment1.xml布局文件并返回。同樣的方法,我們?cè)賹?xiě)好Fragment2 :

public class Fragment2 extends Fragment {
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, container, false);
	}
 
}

然后打開(kāi)或新建activity_main.xml作為主Activity的布局文件,在里面加入兩個(gè)Fragment的引用,使用android:name前綴來(lái)引用具體的Fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" >
 
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragmentdemo.Fragment1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />
 
    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragmentdemo.Fragment2"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />
 
</LinearLayout>

最后打開(kāi)或新建MainActivity作為程序的主Activity,里面的代碼非常簡(jiǎn)單,都是自動(dòng)生成的:

public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
 
}

現(xiàn)在我們來(lái)運(yùn)行一次程序,就會(huì)看到,一個(gè)Activity很融洽地包含了兩個(gè)Fragment,這兩個(gè)Fragment平分了整個(gè)屏幕,效果圖如下:

動(dòng)態(tài)添加Fragment

你已經(jīng)學(xué)會(huì)了如何在XML中使用Fragment,但是這僅僅是Fragment最簡(jiǎn)單的功能而已。Fragment真正的強(qiáng)大之處在于可以動(dòng)態(tài)地添加到Activity當(dāng)中,因此這也是你必須要掌握的東西。當(dāng)你學(xué)會(huì)了在程序運(yùn)行時(shí)向Activity添加Fragment,程序的界面就可以定制的更加多樣化。下面我們立刻來(lái)看看,如何動(dòng)態(tài)添加Fragment。

還是在上一節(jié)代碼的基礎(chǔ)上修改,打開(kāi)activity_main.xml,將其中對(duì)Fragment的引用都刪除,只保留最外層的LinearLayout,并給它添加一個(gè)id,因?yàn)槲覀円獎(jiǎng)討B(tài)添加Fragment,不用在XML里添加了,刪除后代碼如下:

<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" >
 
</LinearLayout>

然后打開(kāi)MainActivity,修改其中的代碼如下所示:

public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		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();
		}
	}
 
}

首先,我們要獲取屏幕的寬度和高度,然后進(jìn)行判斷,如果屏幕寬度大于高度就添加fragment1,如果高度大于寬度就添加fragment2。動(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方法提交。

現(xiàn)在運(yùn)行一下程序,效果如下圖所示:

如果你是在使用模擬器運(yùn)行,按下ctrl + F11切換到豎屏模式。效果如下圖所示:

Fragment的生命周期

和Activity一樣,F(xiàn)ragment也有自己的生命周期,理解Fragment的生命周期非常重要,我們通過(guò)代碼的方式來(lái)瞧一瞧Fragment的生命周期是什么樣的:

public class Fragment1 extends Fragment {
	public static final String TAG = "Fragment1";
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		return inflater.inflate(R.layout.fragment1, container, false);
	}
 
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		Log.d(TAG, "onAttach");
	}
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
	}
 
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "onActivityCreated");
	}
 
	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}
 
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
	}
 
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
 
	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}
 
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView");
	}
 
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
 
	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach");
	}
 
}

可以看到,上面的代碼在每個(gè)生命周期的方法里都打印了日志,然后我們來(lái)運(yùn)行一下程序,可以看到打印日志如下:

這時(shí)點(diǎn)擊一下home鍵,打印日志如下:

如果你再重新進(jìn)入進(jìn)入程序,打印日志如下:

然后點(diǎn)擊back鍵退出程序,打印日志如下:

看到這里,我相信大多數(shù)朋友已經(jīng)非常明白了,因?yàn)檫@和Activity的生命周期太相似了。只是有幾個(gè)Activity中沒(méi)有的新方法,這里需要重點(diǎn)介紹一下:

  • onAttach方法:Fragment和Activity建立關(guān)聯(lián)的時(shí)候調(diào)用。
  • onCreateView方法:為Fragment加載布局時(shí)調(diào)用。
  • onActivityCreated方法:當(dāng)Activity中的onCreate方法執(zhí)行完后調(diào)用。
  • onDestroyView方法:Fragment中的布局被移除時(shí)調(diào)用。
  • onDetach方法:Fragment和Activity解除關(guān)聯(lián)的時(shí)候調(diào)用。

Fragment之間進(jìn)行通信

通常情況下,Activity都會(huì)包含多個(gè)Fragment,這時(shí)多個(gè)Fragment之間如何進(jìn)行通信就是個(gè)非常重要的問(wèn)題了。我們通過(guò)一個(gè)例子來(lái)看一下,如何在一個(gè)Fragment中去訪問(wèn)另一個(gè)Fragment的視圖。

還是在第一節(jié)代碼的基礎(chǔ)上修改,首先打開(kāi)fragment2.xml,在這個(gè)布局里面添加一個(gè)按鈕:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffff00" >
 
    <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="Get fragment1 text"
        />
 
</LinearLayout>

然后打開(kāi)fragment1.xml,為T(mén)extView添加一個(gè)id:

<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/fragment1_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fragment 1"
        android:textColor="#000000"
        android:textSize="25sp" />
 
</LinearLayout>

接著打開(kāi)Fragment2.java,添加onActivityCreated方法,并處理按鈕的點(diǎn)擊事件:

public class Fragment2 extends Fragment {
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, container, false);
	}
 
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Button button = (Button) getActivity().findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView textView = (TextView) getActivity().findViewById(R.id.fragment1_text);
				Toast.makeText(getActivity(), textView.getText(), Toast.LENGTH_LONG).show();
			}
		});
	}
 
}

現(xiàn)在運(yùn)行一下程序,并點(diǎn)擊一下fragment2上的按鈕,效果如下圖所示:

我們可以看到,在fragment2中成功獲取到了fragment1中的視圖,并彈出Toast。這是怎么實(shí)現(xiàn)的呢?主要都是通過(guò)getActivity這個(gè)方法實(shí)現(xiàn)的。getActivity方法可以讓Fragment獲取到關(guān)聯(lián)的Activity,然后再調(diào)用Activity的findViewById方法,就可以獲取到和這個(gè)Activity關(guān)聯(lián)的其它Fragment的視圖了。

以上就是Android Fragment使用全解的詳細(xì)內(nèi)容,更多關(guān)于Android Fragment使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)

    Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)

    最近需要實(shí)現(xiàn)一個(gè)手機(jī)通訊錄的快速索引功能。根據(jù)姓名首字母快速索引功能,下面通過(guò)本篇文章給大家介紹Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)的相關(guān)代碼,需要的朋友參考下
    2015-12-12
  • Android 性能優(yōu)化系列之bitmap圖片優(yōu)化

    Android 性能優(yōu)化系列之bitmap圖片優(yōu)化

    在日常開(kāi)發(fā)的APP,大部分時(shí)候需要想用戶展示圖片信息,圖片最終對(duì)應(yīng)Android中的Bitmap對(duì)象。而對(duì)于APP端來(lái)說(shuō)Bitmap又是一個(gè)比較麻煩的問(wèn)題,主要表現(xiàn)在Bitmap是非常占用內(nèi)存的對(duì)象,處理不當(dāng)將導(dǎo)致APP運(yùn)行卡頓甚至出現(xiàn)OOM
    2021-11-11
  • Android?WebRTC?對(duì)?AudioRecord?的使用技術(shù)分享

    Android?WebRTC?對(duì)?AudioRecord?的使用技術(shù)分享

    這篇文章主要介紹了Android?WebRTC?對(duì)?AudioRecord?的使用技術(shù)分享,AudioRecord?是?Android?基于原始PCM音頻數(shù)據(jù)錄制的類(lèi),接下來(lái)和小編進(jìn)入文章了解更詳細(xì)的內(nèi)容吧
    2022-02-02
  • android自定義View實(shí)現(xiàn)簡(jiǎn)單五子棋游戲

    android自定義View實(shí)現(xiàn)簡(jiǎn)單五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了android自定義View實(shí)現(xiàn)簡(jiǎn)單五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android中關(guān)于FragmentA嵌套FragmentB的問(wèn)題

    Android中關(guān)于FragmentA嵌套FragmentB的問(wèn)題

    這篇文章主要為大家詳細(xì)介紹了Android中關(guān)于FragmentA嵌套FragmentB的問(wèn)題,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android 判斷程序在前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行

    Android 判斷程序在前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行

    本文主要介紹了Android 判斷程序在前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • Android實(shí)現(xiàn)EditText換行自動(dòng)縮進(jìn)功能

    Android實(shí)現(xiàn)EditText換行自動(dòng)縮進(jìn)功能

    在很多需要輸入多行文本的應(yīng)用(如記事本、編程代碼編輯器、博客編輯器等)中,自動(dòng)縮進(jìn)功能能大大提升用戶的編輯效率與體驗(yàn),本文給大家介紹了Android實(shí)現(xiàn)EditText換行自動(dòng)縮進(jìn)功能,下面提供整合后的完整代碼示例,需要的朋友可以參考下
    2025-04-04
  • Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼

    Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼

    這篇文章主要介紹了Android 中對(duì)JSON數(shù)據(jù)解析實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android SearchView搜索框組件的使用方法

    Android SearchView搜索框組件的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android SearchView搜索框組件的使用方法,即時(shí)搜索提示功能的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android實(shí)現(xiàn)TextView顯示HTML加圖片的方法

    Android實(shí)現(xiàn)TextView顯示HTML加圖片的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)TextView顯示HTML加圖片的方法,結(jié)合實(shí)例形式分析了TextView控件顯示網(wǎng)絡(luò)圖片的相關(guān)操作技巧,需要的朋友可以參考下
    2016-07-07

最新評(píng)論

芜湖县| 宾阳县| 安远县| 都江堰市| 扶余县| 睢宁县| 五河县| 海丰县| 纳雍县| 育儿| 迭部县| 报价| 武义县| 东阳市| 永清县| 潼南县| 抚松县| 黄大仙区| 大新县| 阿巴嘎旗| 旺苍县| 临猗县| 高碑店市| 堆龙德庆县| 桑植县| 赞皇县| 大洼县| 开平市| 凤山市| 北安市| 湖南省| 柘城县| 镇康县| 嫩江县| 余江县| 祁东县| 保亭| 镇巴县| 武安市| 新乐市| 双柏县|