Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 自訂圓角 ListView:讓列表四角落下圓弧的完整做法

編輯:Android 開發實例

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

一、自訂 CornerListView

/**
 * 圓角 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 圖形。特別處理了只有一筆資料時的四角全圓狀況。

二、四個 selector 資源

app_list_corner_round(四角圓)

<?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>

app_list_corner_round_top(上方兩角圓)

<?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>

app_list_corner_round_bottom(下方兩角圓)

<?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>

app_list_corner_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,一次解決而不必逐項切換。

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