編輯:Android 開發實例
系統內建的 ListView 是矩形,點擊回饋也是整齊的方塊。若希望列表呈現「上圓角、下圓角」的卡片造型,需要自訂一個 ListView 子類,依點擊位置切換不同的 selector 背景。

/**
* 圓角 ListView 示例
* @FileName: CornerListView.java
*/
public class CornerListView extends ListView {
public CornerListView(Context context) {
super(context);
}
public CornerListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CornerListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
int x = (int) ev.getX();
int y = (int) ev.getY();
int itemnum = pointToPosition(x, y);
if (itemnum == AdapterView.INVALID_POSITION)
break;
else {
if (itemnum == 0) {
if (itemnum == (getAdapter().getCount() - 1)) {
// 只有一筆資料:四個角都要圓
setSelector(R.drawable.app_list_corner_round);
} else {
// 第一筆:上方兩角圓
setSelector(R.drawable.app_list_corner_round_top);
}
} else if (itemnum == (getAdapter().getCount() - 1)) {
// 最後一筆:下方兩角圓
setSelector(R.drawable.app_list_corner_round_bottom);
} else {
// 中間:不做圓角
setSelector(R.drawable.app_list_corner_shape);
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onInterceptTouchEvent(ev);
}
}
核心邏輯是攔截 ACTION_DOWN 事件,用 pointToPosition() 換算出被點擊的項目索引,再依「第一筆/最後一筆/中間」三種情況套用不同的 selector 圖形。特別處理了只有一筆資料時的四角全圓狀況。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"
android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
<corners android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient android:startColor="#BFEEFF"
android:endColor="#40B9FF"
android:angle="270"/>
</shape>
把 CornerListView 寫進版面配置,寫法與一般 ListView 完全相同,只是把標籤換成完整類別名稱:
<com.example.widget.CornerListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
· <corners> 中未指定的角預設為 0 半徑,因此只寫上方兩角就能得到「上圓下平」的效果。
· android:angle="270" 讓漸層由上往下。
· 在 onInterceptTouchEvent 中呼叫 setSelector 會影響事件攔截行為,若列表需要與滾動、下拉等操作共存,建議改為在 getView 或背景 selector 中處理,並實測滑動是否順暢。
· 更現代的替代做法是使用 CardView 或直接為 item 設定 backgroundTint 與 shape,並以 RecyclerView 搭配 ItemDecoration 呈現卡片間距。
· 若整個列表外觀都要圓角,也可以為 ListView 設定 android:clipToOutline 搭配 outlineProvider,一次解決而不必逐項切換。
Android MediaPlayer:打造自己的音樂播放器
要在 Android 上播放音訊或影片,最直接的方式就是使用 MediaPlayer 類別。它是系統內建媒體播放服務的 Java 介面,音訊、影片、串流都吃得住,同時 a
Android 載入 Spinner 進度指示實作教學
在 Android 裡,當某項工作正在進行、想讓使用者知道「系統還在忙」,就可以用進度條來表達。進度條大致有兩種形式:帶有進度的水平進度列,以及持續旋轉的載入轉圈(Spi
Android 載入 Spinner:實作不知何時結束的等待提示
當 App 執行某項工作時,若能給使用者一個「正在處理中」的視覺回饋,體驗會好很多。Android 的進度條有兩種型態:一種是 Hamburg 風格的水平進度列,會顯示百
Android JSON 解析器完整實作
JSON 是 JavaScript Object Notation 的縮寫,它是一種與語言無關的資料交換格式,被廣泛視為 XML 的理想替代品。本章將說明如何在 Andr