国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

使用Fragment來處理Andoird app的UI布局的實例分享

2020-04-11 10:44:49
字體:
來源:轉載
供稿:網友

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

2016225104542958.png (240×400)2016225104609056.png (240×400)

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

2016225104636405.jpg (253×599)

②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);   }     } 

 
其他的部分就是一些數組,String的定義了,這個demo雖然簡單,卻將Android Fragment方面常用到的做了一個綜述,如果自己寫明白了這個的話,今后遇到類似的項目應該要好應付的多,好了,收工!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绍兴县| 屯留县| 灌云县| 彩票| 铜川市| 太和县| 昆明市| 车险| 印江| 珠海市| 通州区| 内黄县| 邵武市| 思茅市| 宁强县| 和田市| 藁城市| 农安县| 军事| 扶风县| 滕州市| 馆陶县| 来凤县| 龙川县| 龙岩市| 冷水江市| 安义县| 安新县| 岢岚县| 福鼎市| 蓬莱市| 那坡县| 新源县| 中阳县| 密云县| 尚志市| 黄平县| 咸丰县| 万盛区| 辽宁省| 中超|