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

自訂 ArrayAdapter:用 Java 物件當資料型別實作圖文列表

編輯:關於 Android 開發

自訂 ArrayAdapter:用 Java 物件當資料型別實作圖文列表

一般教學裡 ArrayAdapter 綁定的都是 String,但真實專案幾乎不可能只顯示純文字。本篇示範如何自訂一個以 Java 物件為資料型別的 ArrayAdapter,做出「左圖右文」的列表效果,做法與 SimpleAdapter 的思路一致,但控制力更強。

一、定義資料模型

package com.example.news;

public class News {
    private String title;
    private String content;
    private int imageId;

    News(String title, String content, int imageId) {
        this.title = title;
        this.content = content;
        this.imageId = imageId;
    }

    public String getTitle() { return title; }
    public String getContent() { return content; }
    public int getimageId() { return imageId; }
    public void setTitle(String title) { this.title = title; }
    public void setContent(String content) { this.content = content; }
}

二、列表項版面配置

<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/news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/news_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

三、自訂 NewsAdapter

繼承 ArrayAdapter<News>,並覆寫 getView()。建構子中的 resource 就是列表項的版面配置 ID:

package com.example.news;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAdapter extends ArrayAdapter<News> {
    private int resourceId;

    public NewsAdapter(Context context, int resource, List<News> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        News news = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        } else {
            view = convertView;
        }
        TextView news_title = (TextView) view.findViewById(R.id.news_title);
        ImageView news_image = (ImageView) view.findViewById(R.id.news_image);
        news_title.setText(news.getTitle());
        news_image.setImageResource(news.getimageId());
        return view;
    }
}

這裡有個關鍵細節:inflate() 的第二個參數傳 null 雖然能跑,但會讓 Android Studio 發出警告,而且失去版面參數(LayoutParams)的套用。比較好的寫法是傳 parent 並加上 false:inflate(resourceId, parent, false)。

四、主版面與 Activity

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView android:id="@+id/list_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    private ListView list_title;
    private List<News> list = new ArrayList<News>();
    private NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list_title = (ListView) findViewById(R.id.list_title);
        initList();
        adapter = new NewsAdapter(this, R.layout.news_item, list);
        list_title.setAdapter(adapter);
    }

    private void initList() {
        News news1 = new News("標題1", "1", R.drawable.ic_launcher);
        list.add(news1);
        News news2 = new News("標題2", "2", R.drawable.ic_launcher);
        list.add(news2);
        News news3 = new News("標題3", "3", R.drawable.ic_launcher);
        list.add(news3);
        News news4 = new News("標題4", "4", R.drawable.ic_launcher);
        list.add(news4);
    }
}

五、convertView 複用機制

getView() 裡的 convertView 是理解 ListView 效能的關鍵。ListView 只會為「畫面上看得見的項目」建立 View,當某個項目滾出螢幕,它的 View 會被回收並作為 convertView 傳給下一個即將顯示的項目,避免重複 inflate。這就是為什麼一定要寫 if (convertView == null) 判斷——它讓 inflate 的次數從「資料筆數」降到接近「一次螢幕可容納的筆數」。

如果列表項結構比較複雜,還可以進一步用 ViewHolder 模式:建立一個內部類持有各子 View 的參照,把它掛在 view.setTag() 上,下次複用時直接取出,省下 findViewById() 的呼叫次數。

至於更現代的方案,RecyclerView 把這套複用邏輯強制內建為 ViewHolder,並支援橫向、網格、瀑布流等多種排版,新專案建議直接採用。

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