使用Fragment來處理Andoird app的UI布局的實例分享
Fragment 的出現(xiàn)一方面是為了緩解 Activity 任務過重的問題,另一方面是為了處理在不同屏幕上 UI 組件的布局問題,而且它還提供了一些新的特性(例如 Retainable)來處理一些在 Activity 中比較棘手的問題。Fragment 擁有和 Activity 一致的生命周期,它和 Activity 一樣被定義為 Controller 層的類。有過中大型項目開發(fā)經(jīng)驗的開發(fā)者,應該都會遇到過 Activity 過于臃腫的情況,而 Fragment 的出現(xiàn)就是為了緩解這一狀況,可以說 它將屏幕分解為多個「Fragment(碎片)」(這句話很重要),但它又不同于 View,它干的實質上就是 Activity 的事情,負責控制 View 以及它們之間的邏輯。將屏幕碎片化為多個 Fragment 后,其實 Activity 只需要花精力去管理當前屏幕內應該顯示哪些 Fragments,以及應該對它們進行如何布局就行了。這是一種組件化的思維,用 Fragment 去組合了一系列有關聯(lián)的 UI 組件,并管理它們之間的邏輯,而 Activity 負責在不同屏幕下(例如橫豎屏)布局不同的 Fragments 組合。這種碎片不單單能管理可視的 Views,它也能執(zhí)行不可視的 Tasks,它提供了 retainInstance 屬性,能夠在 Activity 因為屏幕狀態(tài)發(fā)生改變(例如切換橫豎屏時)而銷毀重建時,依然保留實例。這示意著我們能在 RetainedFragment 里面執(zhí)行一些在屏幕狀態(tài)發(fā)生改變時不被中斷的操作。
下面我們就來具體看一個Android Fragment功能的例子。
實現(xiàn)的功能很簡單,也是最基本的,上下分別是兩個Fragment,上面的Fragment中是一個listview,當點擊item時,下面的Fragment顯示對應的文字詳細信息:
具體的實現(xiàn)步驟如下:
①創(chuàng)建工程FragmentExam,目錄視圖如下(把之前的FragmentPreference的demo也加到了一起):
②main.xml文件布局,垂直方向上兩個Fragment,用<Fragment>標簽聲明
<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方法將布局頁面引進:
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的代碼,這個比較簡單,里面有一個設置TextView內容的方法,其布局頁面也僅僅是一個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的定義了,這個demo雖然簡單,卻將Android Fragment方面常用到的做了一個綜述,如果自己寫明白了這個的話,今后遇到類似的項目應該要好應付的多,好了,收工!
- Android應用開發(fā)中Fragment間通信的實現(xiàn)教程
- 淺談Android app開發(fā)中Fragment的Transaction操作
- Android app開發(fā)中的Fragment入門學習教程
- Android的Fragment的生命周期各狀態(tài)和回調函數(shù)使用
- 實例講解Android應用開發(fā)中Fragment生命周期的控制
- Android中Fragment的生命周期與返回棧的管理
- 實例探究Android應用編寫時Fragment的生命周期問題
- Android中Fragment與Activity的生命周期對比
- Android應用開發(fā)中Fragment的靜態(tài)加載與動態(tài)加載實例
- FrameLayout和Fragment處理Android應用UI布局實例
- Android應用UI開發(fā)中Fragment的常見用法小結
- Android應用開發(fā)中使用Fragment的入門學習教程
- Android應用開發(fā)中Fragment存儲功能的基本用法
- 淺談Android App開發(fā)中Fragment的創(chuàng)建與生命周期
相關文章
Flutter中嵌入Android 原生TextView實例教程
這篇文章主要給大家介紹了關于Flutter中嵌入Android 原生TextView的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09Android 中menu同時顯示圖標和文字的實現(xiàn)
這篇文章主要介紹了Android 中menu同時顯示圖標和文字的實現(xiàn)的相關資料,希望通過本文能幫助到大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10Android模仿實現(xiàn)微博詳情頁滑動固定頂部欄的效果實例
這篇文章主要給大家介紹了關于利用Android模仿實現(xiàn)微博詳情頁滑動固定頂部欄效果的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。2017-11-11ANDROID BottomNavigationBar底部導航欄的實現(xiàn)示例
本篇文章主要介紹了ANDROID BottomNavigationBar底部導航欄的實現(xiàn)示例,非常具有實用價值,需要的朋友可以參考下2017-10-10python gstreamer實現(xiàn)視頻快進/快退/循環(huán)播放功能
這篇文章主要介紹了python gstreamer 實現(xiàn)視頻快進/快退/循環(huán)播放功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android DrawerLayout帶有側滑功能的布局類(2)
這篇文章主要為大家詳細介紹了Android DrawerLayout帶有側滑功能的布局類,感興趣的小伙伴們可以參考一下2016-07-07