使用Fragment來處理Andoird app的UI布局的實(shí)例分享
Fragment 的出現(xiàn)一方面是為了緩解 Activity 任務(wù)過重的問題,另一方面是為了處理在不同屏幕上 UI 組件的布局問題,而且它還提供了一些新的特性(例如 Retainable)來處理一些在 Activity 中比較棘手的問題。Fragment 擁有和 Activity 一致的生命周期,它和 Activity 一樣被定義為 Controller 層的類。有過中大型項(xiàng)目開發(fā)經(jīng)驗(yàn)的開發(fā)者,應(yīng)該都會遇到過 Activity 過于臃腫的情況,而 Fragment 的出現(xiàn)就是為了緩解這一狀況,可以說 它將屏幕分解為多個(gè)「Fragment(碎片)」(這句話很重要),但它又不同于 View,它干的實(shí)質(zhì)上就是 Activity 的事情,負(fù)責(zé)控制 View 以及它們之間的邏輯。將屏幕碎片化為多個(gè) Fragment 后,其實(shí) Activity 只需要花精力去管理當(dāng)前屏幕內(nèi)應(yīng)該顯示哪些 Fragments,以及應(yīng)該對它們進(jìn)行如何布局就行了。這是一種組件化的思維,用 Fragment 去組合了一系列有關(guān)聯(lián)的 UI 組件,并管理它們之間的邏輯,而 Activity 負(fù)責(zé)在不同屏幕下(例如橫豎屏)布局不同的 Fragments 組合。這種碎片不單單能管理可視的 Views,它也能執(zhí)行不可視的 Tasks,它提供了 retainInstance 屬性,能夠在 Activity 因?yàn)槠聊粻顟B(tài)發(fā)生改變(例如切換橫豎屏?xí)r)而銷毀重建時(shí),依然保留實(shí)例。這示意著我們能在 RetainedFragment 里面執(zhí)行一些在屏幕狀態(tài)發(fā)生改變時(shí)不被中斷的操作。
下面我們就來具體看一個(gè)Android Fragment功能的例子。
實(shí)現(xiàn)的功能很簡單,也是最基本的,上下分別是兩個(gè)Fragment,上面的Fragment中是一個(gè)listview,當(dāng)點(diǎn)擊item時(shí),下面的Fragment顯示對應(yīng)的文字詳細(xì)信息:


具體的實(shí)現(xiàn)步驟如下:
①創(chuàng)建工程FragmentExam,目錄視圖如下(把之前的FragmentPreference的demo也加到了一起):

②main.xml文件布局,垂直方向上兩個(gè)Fragment,用<Fragment>標(biāo)簽聲明
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#7ecef4">
<fragment
android:name="com.example.fragementexam.FragementList"
android:id="@+id/frag_list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<fragment
android:name="com.example.fragementexam.FragementDetails"
android:id="@+id/frag_detail"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
③FragmentList.java的代碼,它繼承了ListFragment,注意在onCreateView方法中使用inflater的inflate方法將布局頁面引進(jìn):
package com.example.fragementexam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class FragementList extends ListFragment {
private String[] values = new String[] { "侏儒", "人類", "暗夜精靈", "矮人", "德萊尼",
"狼人" };
private int[] images = new int[] { R.drawable.gnome,
R.drawable.human, R.drawable.nightelf,
R.drawable.dwarf, R.drawable.draenei,
R.drawable.werewolf };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < values.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("values", values[i]);
listItem.put("images", images[i]);
listItems.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), listItems,
R.layout.list_item, new String[] { "values", "images" },
new int[] { R.id.txt_title, R.id.img });
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// String item = (String) getListAdapter().getItem(position);
FragementDetails frag = (FragementDetails) getFragmentManager()
.findFragmentById(R.id.frag_detail);
if (frag != null && frag.isInLayout()) {
switch (position) {
case 0:
frag.setText(getString(R.string.Gnome));
break;
case 1:
frag.setText(getString(R.string.Human));
break;
case 2:
frag.setText(getString(R.string.NightElf));
break;
case 3:
frag.setText(getString(R.string.Dwarf));
break;
case 4:
frag.setText(getString(R.string.Draenei));
break;
case 5:
frag.setText(getString(R.string.Werewolf));
break;
}
}
Log.i("PDA", "position = " + position);
}
}
④FragementDetails.java的代碼,這個(gè)比較簡單,里面有一個(gè)設(shè)置TextView內(nèi)容的方法,其布局頁面也僅僅是一個(gè)TextView
package com.example.fragementexam;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragementDetails extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag_detail, container,false);
}
public void setText(String item){
TextView txt = (TextView) getView().findViewById(R.id.txt_detail);
txt.setText(item);
}
}
其他的部分就是一些數(shù)組,String的定義了,這個(gè)demo雖然簡單,卻將Android Fragment方面常用到的做了一個(gè)綜述,如果自己寫明白了這個(gè)的話,今后遇到類似的項(xiàng)目應(yīng)該要好應(yīng)付的多,好了,收工!
- Android應(yīng)用開發(fā)中Fragment間通信的實(shí)現(xiàn)教程
- 淺談Android app開發(fā)中Fragment的Transaction操作
- Android app開發(fā)中的Fragment入門學(xué)習(xí)教程
- Android的Fragment的生命周期各狀態(tài)和回調(diào)函數(shù)使用
- 實(shí)例講解Android應(yīng)用開發(fā)中Fragment生命周期的控制
- Android中Fragment的生命周期與返回棧的管理
- 實(shí)例探究Android應(yīng)用編寫時(shí)Fragment的生命周期問題
- Android中Fragment與Activity的生命周期對比
- Android應(yīng)用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實(shí)例
- FrameLayout和Fragment處理Android應(yīng)用UI布局實(shí)例
- Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)
- Android應(yīng)用開發(fā)中使用Fragment的入門學(xué)習(xí)教程
- Android應(yīng)用開發(fā)中Fragment存儲功能的基本用法
- 淺談Android App開發(fā)中Fragment的創(chuàng)建與生命周期
相關(guān)文章
android重力感應(yīng)開發(fā)之微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Flutter中嵌入Android 原生TextView實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Flutter中嵌入Android 原生TextView的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android 中menu同時(shí)顯示圖標(biāo)和文字的實(shí)現(xiàn)
這篇文章主要介紹了Android 中menu同時(shí)顯示圖標(biāo)和文字的實(shí)現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
Android模仿實(shí)現(xiàn)微博詳情頁滑動固定頂部欄的效果實(shí)例
這篇文章主要給大家介紹了關(guān)于利用Android模仿實(shí)現(xiàn)微博詳情頁滑動固定頂部欄效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
ANDROID BottomNavigationBar底部導(dǎo)航欄的實(shí)現(xiàn)示例
本篇文章主要介紹了ANDROID BottomNavigationBar底部導(dǎo)航欄的實(shí)現(xiàn)示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
python gstreamer實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放功能
這篇文章主要介紹了python gstreamer 實(shí)現(xiàn)視頻快進(jìn)/快退/循環(huán)播放功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android DrawerLayout帶有側(cè)滑功能的布局類(2)
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類,感興趣的小伙伴們可以參考一下2016-07-07

