Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android ListFragment 觀念與實戰範例

編輯:Android 開發實例

ListFragment 是什麼

ListFragment 繼承自 Fragment,因此具備 Fragment 的所有特性:它可以作為 Activity 的一部分存在,讓頁面組裝更為靈活。與一般 Fragment 的差別在於,它的內容以列表形式呈現,預設布局就包含一個 ListView。

由此衍生出一個硬性規則:ListFragment 對應的布局檔中,必須有一個 id 為 @android:id/list 的 ListView,否則會出錯。

基礎用法

一、Activity 程式碼

public class FragmentTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

二、Activity 布局

<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>

這個 Activity 的布局只放了一個 Fragment,接著看它的實作。

三、MyListFragment

public class MyListFragment extends ListFragment {
    private static final String TAG = "##MyListFragment##";
    private ListView selfList;
    String[] cities = { "Shenzhen", "Beijing", "Shanghai", "Guangzhou", "Wuhan",
            "Tianjing", "Changsha", "Xi'an", "Chongqing", "Guilin", };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        return inflater.inflate(R.layout.list_fragment, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        // 設定 ListFragment 預設的 ListView,也就是 @id/android:list
        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) {
        Log.d(TAG, "onListItemClick");
        Toast.makeText(getActivity(), "You have selected " + cities[position],
                Toast.LENGTH_SHORT).show();
    }
}

這裡使用系統內建的 android.R.layout.simple_list_item_1 作為每一列的樣板。

四、list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- ListFragment 對應的 id 固定為 "@id/android:list" -->
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />
</LinearLayout>

改用 SimpleAdapter 承載多欄資料

如果每一列要顯示的不只一個欄位,可以改用 SimpleAdapter,把資料的 key 與自訂布局中的控制項 id 對應起來。

public class MyListFragment extends ListFragment {
    @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) {
        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(
                getActivity(), getSimpleData(), R.layout.item, 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();
    }

    private List<Map<String, Object>> getSimpleData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "Ferris wheel");
        map.put("info", "Suzhou Ferris wheel");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("title", "Flower");
        map.put("info", "Roser");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("title", "Disk");
        map.put("info", "Song Disk");
        list.add(map);
        return list;
    }
}

item.xml 的內容就是兩個 TextView:

<?xml version="1.0" encoding="utf-8"?>
<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:左邊用系統內建樣板顯示城市清單,右邊則用自訂布局顯示「標題 + 說明」兩行文字。

<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 使用系統樣板:

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 則改用 SimpleAdapter 搭配自訂的 two_textview 布局:

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

    private List<Map<String, Object>> getSimpleData() {
        // 組出 title / info 的對應資料,略
        return null;
    }
}

三個布局檔的重點一致:ListFragment 的 ListView id 一律寫成 @id/android:list,差別只在於列的樣板是系統內建或自訂。

執行結果

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