Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ListFragment 實戰:Android 列表 Fragment 的基礎與進階用法

編輯:關於 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> 

進階實例:一個畫面放兩個 ListFragment

這個實例建立一個 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 從基礎到進階的完整用法,希望對你的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved