編輯:關於 Android 程式設計
ListFragment 繼承自 Fragment,因此具備 Fragment 的特性,能作為 Activity 的一部分,讓頁面設計更靈活。與 Fragment 不同的是,它的內容以清單(list)形式呈現,預設內含一個 ListView。因此,在 ListFragment 對應的版面檔中,必須指定一個 android:id 為「@android:id/list」的 ListView 控制項。
1. Activity 程式碼
public class FragmentTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
2. Activity 版面:這裡只放了一個 Fragment。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:name="com.skw.fragmenttest.MyListFragment" android:id="@+id/myfragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
3. MyListFragment:自訂的 ListFragment,使用 list_fragment.xml 作為版面,並用 android.R.layout.simple_list_item_1 呈現每一列。
public class MyListFragment extends ListFragment { private static final String TAG = "##MyListFragment##"; String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.list_fragment, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities)); } public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), "You have selected " + cities[position], Toast.LENGTH_SHORT).show(); } }
4. list_fragment.xml:注意 ListView 的 id 固定為「@id/android:list」。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false" /> </LinearLayout>
若改用 SimpleAdapter,可把 from 與 to 對應起來,讓每一列顯示自訂版面(這裡每一列用 R.layout.item):
@Override public void onCreate(Bundle savedInstanceState) { final String[] from = new String[] {"title", "info"}; final int[] to = new int[] {R.id.text1, R.id.text2}; super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter( this.getActivity(), getSimpleData(), R.layout.item, from, to); this.setListAdapter(adapter); }
item.xml 每一列放兩個 TextView:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text1" android:textSize="12sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/text2" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
這個實例建立一個 Activity,內含兩個 ListFragment:第一個用系統內建的 simple_list_item_1 顯示,第二個用自訂版面顯示,每一列兩個文字。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:name="com.skywang.app.ListFragmentImpl" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:name="com.skywang.app.ListFragmentSelf" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Activity 程式碼:
public class ListFragmentTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_fragment_test); } }
ListFragmentImpl:在 onCreateView 使用 list_fragment_impl 作為版面,並用 setListAdapter 指定 simple_list_item_1 與 cities 資料。
public class ListFragmentImpl extends ListFragment{ String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan", "Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.list_fragment_impl, container, false); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, cities)); } public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), "You have selected " + cities[position], Toast.LENGTH_SHORT).show(); } }
ListFragmentSelf:在 onCreate 用 setListAdapter 指定 R.layout.two_textview 作為每一列的版面,並以 SimpleAdapter 對應 from/to。
public class ListFragmentSelf extends ListFragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.list_fragment_self, container, false); } @Override public void onCreate(Bundle savedInstanceState) { final String[] from = new String[] {"title", "info"}; final int[] to = new int[] {R.id.text1, R.id.text2}; super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter( this.getActivity(), getSimpleData(), R.layout.two_textview, from, to); this.setListAdapter(adapter); } public void onListItemClick(ListView parent, View v, int position, long id) { Toast.makeText(getActivity(), "You have selected " + position, Toast.LENGTH_SHORT).show(); } }
two_textview.xml 與前面的 item.xml 結構相同,都是上下兩個 TextView。
完成後的效果如下:

以上就是 ListFragment 從基礎到進階的完整用法,希望對你的學習有所幫助。
Android 進階:SurfaceView 與多執行緒搭配的動畫優化
前面簡單介紹過 SurfaceView 的基本用法,這次談談它和但要執行緒的組合。之所以要混搭,是為了避免動畫閃爍——這其實是一種相當典型的雙執行緒應用寫法。Androi
Android 電商搜索功能實戰:軟鍵盤搜索、延遲自動搜索與按時間排序的歷史記錄
到新公司接手的頭一個任務,是在一個電商模塊裡實現搜索功能與搜索歷史記錄。需求和淘寶這類電商大同小異:頂部一個搜索框,下方展示歷史記錄;用戶在 EditText 裡輸入關鍵
Android 優化之 ArrayMap:原理、比較與適用場景
ArrayMap 是 Android 中常見的輕量級鍵值對容器。它並不是用來取代所有 HashMap 場景,而是在特定資料量與記憶體考量下,提供另一種取捨。 官方文件提醒
滑動掛斷:自定義 View 的簡單實現
「滑動解鎖」「滑動掛斷」這類控件在通話界面裡非常常見。它的好處是防誤觸——相比一個大號的紅色掛斷按鈕,需要滑動到底才能確認的操作,能有效避免通話中臉頰誤觸。 實現它只需要