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

Android 樣式布局實戰:打造帶分組標題與內容的 ListView(附源碼)

編輯:關於 Android 程式設計

ListView 是 Android 中最常用的數據展示控件。如果每一行都長得一模一樣,界面會顯得單調乏味。實際項目裡,我們經常需要讓列表呈現「標題行 + 內容行」交替的樣式,例如城市天氣:城市名佔一行,下面跟三天的天氣詳情。

下面以一個 WeatherDemo 為例,說明如何實現這種分組樣式。

實現思路

  1. 準備兩套不同的 item 布局:city_item.xml(城市標題行)與 content_item.xml(天氣內容行)。
  2. 構造一個數據結構,用類型數組標記每一行應該使用哪種布局。
  3. 編寫自定義 Adapter,在 getView 中根據類型用 LayoutInflater 加載對應的布局並返回不同的 View。

一、WeatherAdapter

public class WeatherAdapter extends BaseAdapter {

    private List<HashMap<String, String>> list;
    private Context context;
    private int[] type;   // 標記每一行的類型

    public WeatherAdapter(Context context, List<HashMap<String, String>> list, int[] type) {
        this.context = context;
        this.list = list;
        this.type = type;
    }

    @Override
    public int getCount() { return list.size(); }

    @Override
    public Object getItem(int position) { return list.get(position); }

    @Override
    public long getItemId(int position) { return position; }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View view = null;

        if (type[position] == 0) {
            // 城市標題行
            view = mInflater.inflate(R.layout.city_item, null);
            String cityName = list.get(position).get("data");
            ImageView image = (ImageView) view.findViewById(R.id.weather_image);
            if ("北京".equals(cityName)) {
                image.setImageResource(R.drawable.beijing);
            } else if ("上海".equals(cityName)) {
                image.setImageResource(R.drawable.shanghai);
            } else if ("廣州".equals(cityName)) {
                image.setImageResource(R.drawable.guangzhou);
            } else if ("深圳".equals(cityName)) {
                image.setImageResource(R.drawable.shenzhen);
            }
            TextView city = (TextView) view.findViewById(R.id.city);
            city.setText(cityName);
        } else {
            // 天氣內容行
            view = mInflater.inflate(R.layout.content_item, null);
            String content = list.get(position).get("data");
            String[] items = content.split(",");
            TextView weather = (TextView) view.findViewById(R.id.content);
            weather.setText(items[0] + " 天氣: " + items[1] + "; 溫度: " + items[2]);
            TextView date = (TextView) view.findViewById(R.id.date);
            date.setText(items[3]);
        }
        return view;
    }
}

二、MainActivity

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView) findViewById(R.id.list_items);
        ArrayList<HashMap<String, String>> listItem = new ArrayList<>();

        // 城市與未來三天的天氣數據
        String[] data = {
            "廣州", "今天,晴天,22℃,2014-4-18", "明天,多雲轉陣雨,32~23℃,2014-4-19", "後天,陰轉多雲,33~23℃,2014-4-20",
            "上海", "今天,陣雨轉雷陣雨,31~25℃,2014-4-18", "明天,雷陣雨轉陰,31~24℃,2014-4-19", "後天,陣雨轉多雲,32~27℃,2014-4-20",
            "北京", "今天,中雨轉暴雨,32~25℃,2014-4-18", "明天,暴雨轉大到暴雨,29~25℃,2014-4-19", "後天,暴雨轉陣雨,29~25℃,2014-4-20",
            "深圳", "今天,中雨轉暴雨,31~25℃,2014-4-18", "明天,暴雨,29~24℃,2014-4-19", "後天,大雨轉陣雨,28~25℃,2014-4-20"
        };

        // 0 表示城市標題行,1~3 表示不同日期的內容行
        int[] type = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };

        for (int i = 0; i < data.length; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("data", data[i]);
            listItem.add(map);
        }

        WeatherAdapter adapter = new WeatherAdapter(this, listItem, type);
        list.setAdapter(adapter);
    }
}

三、三個布局文件

city_item.xml(城市標題行)

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#9ACD32"
    android:paddingBottom="4dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip">

    <ImageView
        android:id="@+id/weather_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingTop="4dip"
        android:contentDescription="@string/weather_image" />

    <TextView
        android:id="@+id/city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:textColor="@color/black"
        android:textSize="18sp" />

</RelativeLayout>

content_item.xml(天氣內容行)

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:paddingBottom="4dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip">

    <TextView
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/content"
        android:paddingRight="20dip" />

</RelativeLayout>

main.xml

<LinearLayout 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/list_items"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@color/white"
        android:dividerHeight="1dip" />

</LinearLayout>

小結

這種「多類型 item」的思路非常通用:只要維護好類型數組與數據源的對應關係,就能靈活組合出任意樣式的列表。如果需要進一步優化性能,可以在 getView 中引入 ViewHolder 模式,避免重複的 findViewById 調用。

源碼下載:https://download.csdn.net/detail/yangweixing10/7214057

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