編輯:關於 Android 開發
一般教學裡 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>
繼承 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)。
<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);
}
}

getView() 裡的 convertView 是理解 ListView 效能的關鍵。ListView 只會為「畫面上看得見的項目」建立 View,當某個項目滾出螢幕,它的 View 會被回收並作為 convertView 傳給下一個即將顯示的項目,避免重複 inflate。這就是為什麼一定要寫 if (convertView == null) 判斷——它讓 inflate 的次數從「資料筆數」降到接近「一次螢幕可容納的筆數」。
如果列表項結構比較複雜,還可以進一步用 ViewHolder 模式:建立一個內部類持有各子 View 的參照,把它掛在 view.setTag() 上,下次複用時直接取出,省下 findViewById() 的呼叫次數。
至於更現代的方案,RecyclerView 把這套複用邏輯強制內建為 ViewHolder,並支援橫向、網格、瀑布流等多種排版,新專案建議直接採用。
Android Binder:Service 註冊請求流程解析
Android Binder:Service 註冊請求流程解析 Android 系統大量採用 Service 組件提供核心服務。Service 必須註冊到 Service
Android 手機衛士實作教學(一):打造 Splash 啟動頁
Android 手機衛士實作教學(一):打造 Splash 啟動頁 1. minSdkVersion、targetSdkVersion、maxSdkVersion、ta
Android 開發環境搭建入門:從工具選擇到第一個專案
本文不談太瑣碎的安裝細節(網路上這類教學已經很多),重點放在工具選擇的思路、常見的坑,以及幾個值得收藏的資源站。 開發工具怎麼選 Android 常見的 IDE 有三個
Android 通訊錄字母導航欄源碼解析(一)
本文分享一個仿通訊錄的右側字母索引導航欄完整源碼,實現手指滑動時快速定位聯絡人的效果。 整体分為四個部分:Activity、Adapter、Entity 與自定義 Vi