編輯:關於 Android 程式設計
1. 先談談 Fragment 是什麼
多數人寫 Android 的起點都是 Activity:一個 Activity 配一個 layout,畫面就那麼定了。Fragment 則是架在 Activity 之上的一層,它把原本寫死的 layout 鬆綁——在既有版面中留一個容器,再把新的 layout 動態塞進去。
換句話說,一個 Activity 裡可以同時存在多個可切換的畫面,這正是 Fragment 的價值所在。
2. ListFragment 實作
最終效果:

2.1 先看 Activity 的版面 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <include android:id="@+id/layout_bar" layout="@layout/layout_title"/> <FrameLayout android:id="@+id/fragment_container" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" > </FrameLayout> <include layout="@layout/layout_bottom"/> </LinearLayout>
這個線性版面分成三塊:layout_title、fragment_container、layout_bottom。中間那個 FrameLayout 就是預留給 ListFragment 動態掛載的容器。
2.2 被掛進容器的 fragment_order.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@+id/android:list" android:scrollbars="none" android:dividerHeight="0dp" android:divider="#00000000" android:listSelector="#00000000" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
這裡有個容易踩到的細節:ListFragment 預設會去找系統內建的 list,因此清單控制項的 id 必須寫成 android:list,否則會在執行期出錯。
2.3 接著看 MainActivity 怎麼把 Fragment 掛上去:
private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //********************************************* manager = getFragmentManager(); manager.beginTransaction().add(R.id.fragment_container, homefragment, "article").commit(); //*********************************************
add() 的三個參數分別是:掛載位置(R.id.fragment_container)、Fragment 物件本身、以及給這個 Fragment 取的名字(可任意)。
2.4 Fragment 類別的撰寫:
前面的 fragment_order.xml 就是由這個類別來 inflate。onCreateView 負責產生視圖,onActivityCreated 則把資料綁進清單:
public class Fragment_order extends ListFragment { private MainActivity parentActivity; private String[] values = new String[] { "快餐店", "烤食店", "燒魚店", "甜食店", "蔬菜店", "融合菜店","面條店" }; private int[] images = new int[] { R.drawable.fastfood, R.drawable.roastfood, R.drawable.fishfood, R.drawable.sweetfood, R.drawable.vegetables, R.drawable.multifood,R.drawable.noodles }; //用來初始化listfragmnet每一條項目的資源 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_order, container, false); //這裡用inflate函數,在初始化創建view時返回fragment_order.xml實例 } //下面的部分則是用於將每一條項目的資源放入到listview的每一個條目中去 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); List<Map<String, Object>> list = 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]); list.add(listItem); } SimpleAdapter adapter = new SimpleAdapter(getActivity(),list, R.layout.list_item, new String[] { "values", "images" }, new int[] { R.id.storeName, R.id.storePic }); setListAdapter(adapter); }
這裡最關鍵的是 SimpleAdapter,因為資料與版面必須靠它綁在一起,綁錯就什麼都不會顯示。它的五個參數依序是:(1)當前的 Activity 參照;(2)裝好資料的 list;(3)清單「單一項目」的版面檔,決定外觀;(4)Map 中的鍵名稱陣列,讓 adapter 知道要抓哪些欄位;(5)與上一參數一一對應的 view id,把欄位填進正確的位置。
2.5 最後是單項目的 list_item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" xmlns:app="https://schemas.android.com/apk/res-auto"> <LinearLayout android:id="@+id/contactitem_layout" android:layout_height="65.0dip" android:paddingLeft="12dip" android:background="@drawable/border" android:padding="2dp" android:weightSum="1"> <RelativeLayout android:id="@+id/avatar_container" android:layout_width="match_parent" android:layout_marginTop="4dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" > <ImageView android:id="@+id/storePic" android:layout_width="50.0dip" android:layout_height="50.0dip" android:src="@drawable/head" /> <TextView android:id="@+id/storeName" android:layout_toRightOf="@+id/storePic" android:layout_width="match_parent" android:layout_height="match_parent" android:text="No data" /> </RelativeLayout> </LinearLayout> </LinearLayout>
id 名稱要和 SimpleAdapter 第五個參數完全對得上,這一點別偷懶。新專案裡建議直接用 support-v4 的 Fragment 與 FragmentActivity,getFragmentManager() 那套已屬舊寫法。
Android 變色狀態欄實作:透明狀態欄、Toolbar 與 SystemBarTint
透明、變色與沉浸式狀態欄的差別 在 Android 中,常見說法包含沉浸式狀態欄、透明狀態欄與變色狀態欄。三者常被混用,但概念不同。 沉浸式模式會讓狀態欄與虛擬鍵隱藏,需
Android Activity 與 Service 資料交互實作
在 Android 中,Activity 負責與使用者互動,Service 則適合執行背景任務。當 Service 執行較長時間的操作時,可以透過 Binder 與回呼介
微信怎麼玩?從支付、叫車到發紅包的入門指南
作為手機端的通訊應用,微信早已不只是一個聊天工具。如果你目前還停留在 QQ,還沒開始用微信,那大概會錯過不少便利——行動支付、叫車、發紅包,這些都在同一個 App 裡完成
Android 4.4 KitKat 中 apk 包的安裝流程全解析
說到 apk 包的安裝,4.4 與先前版本其實並無本質差別。在 Android 裡,app 的安裝主要分為四種情況:開機時安裝、adb 命令安裝、從 Google Pla