編輯:關於 Android 開發
ArrayAdapter 是 Android 最基礎的資料綁定工具,但很多人只會用它顯示字串陣列,就再也沒往下深入。這個範例把 ArrayAdapter 包成自家人,做出類似 QQ 的對話視窗:輸入框打字、按發送、訊息往上疊,效果簡單卻把重點都涵蓋了。
運行效果:

系統內建的 ArrayAdapter 只能把一筆資料塞進一個 TextView。但只要覆寫 getView(),你就能完全掌控每一列的長相——客製版面、快取視圖、甚至一列裡放好幾個控制項。這是所有客製化列表的起點。
public class MainActivity extends Activity {
EditText et;
Button btn;
ListView lv;
MyAdapter mMyAdapter;
ArrayList<String> date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = new ArrayList<String>();
et = (EditText) findViewById(R.id.et1);
btn = (Button) findViewById(R.id.btn1);
lv = (ListView) findViewById(R.id.lv);
mMyAdapter = new MyAdapter(this, R.layout.list);
lv.setAdapter(mMyAdapter);
btn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { sendmessage(); }
});
}
public void sendmessage() {
date.add(et.getText().toString());
mMyAdapter.notifyDataSetChanged(); // 資料變了,記得通知
et.setText(null); // 清空輸入框
lv.setSelection(ListView.FOCUS_DOWN); // 捲到最底
}
}
public class MyAdapter extends ArrayAdapter {
private Context context;
private int resource;
public MyAdapter(Context context, int resource) {
super(context, resource);
this.resource = resource;
this.context = context;
}
@Override
public int getCount() {
return date.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater flater = LayoutInflater.from(context);
// 複用 convertView,避免每一列都重新 inflate
if (convertView == null) {
convertView = flater.inflate(resource, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.qqtxt);
tv.setText(date.get(position));
return convertView;
}
}
這段程式碼有三個值得記住的重點:
單列版面用 RelativeLayout,把頭像固定在右側、文字氣泡靠左對齊頭像:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="@+id/ImageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/meimei" />
<TextView android:id="@+id/qqtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ImageView1"
android:background="#ff5252"
android:textSize="20sp" />
</RelativeLayout>
主版面則是「列表在上、輸入列在下」的上下分割,並把分隔線與分隔間距處理掉:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_above="@+id/commentLinearLayout"
android:layout_alignParentTop="true"
android:divider="@android:color/transparent"
android:dividerHeight="15dip"
android:scrollbars="none" />
<LinearLayout android:id="@+id/commentLinearLayout"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#e0e0e0" >
<EditText android:id="@+id/et1"
android:layout_weight="2"
android:hint="請輸入內容" />
<Button android:id="@+id/btn1"
android:text="發送" />
</LinearLayout>
</RelativeLayout>
android:divider 設為透明色再加上 dividerHeight 留白,是做出「無分隔線但有間距」效果的常用小技巧。
這個範例所有訊息都靠右顯示。要做成真正的雙方對話,可以在 getView() 中依資料裡的「是否為自己發送」旗標,切換 inflate 兩種不同的單列版面。再往下的方向還有:用 RecyclerView + ListAdapter 取得更好的動畫與更新效能,以及加上差異更新避免整份列表重繪。
JRebel for Android 1.0 發布:告別頻繁重編譯的開發利器
JRebel for Android 1.0 發布:告別頻繁重編譯的開發利器 什麼是JRebel for Android? 它是一款Android Studio插件,允
Android 與 PHP 的 HTTP 連線實作(三):取得伺服器 JSON 資料
Android 與 PHP 的資料交換概念 Android 與 PHP 的互動,通常透過 HTTP 網路連線完成。PHP 負責連接資料庫、查詢資料,並將結果整理成 JSO
ViewDragHelper 詳解與源碼淺析:打造自訂拖曳容器
Android ViewDragHelper 詳解及源碼淺析 1. 背景 做自訂 ViewGroup 時,「讓子 View 能被手指拖動」幾乎是必經關卡。自己處理 Mo
Android 游戲開發入門:幀動畫的原理與三種資源組織方式
帶你走進游戲開發的世界——這一篇講游戲里最基礎也最常用的表現手段:幀動畫。 一、幀動畫的原理 幀動畫,顧名思義就是「一幀一幀播放的動畫」。它的原理和我們小時候看的動畫片