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

自訂 CornerListView:讓列表item 擁有方形圓角

編輯:關於 Android 程式設計

系統原生的 ListView 在按下時只有一整塊矩形的高亮,放在圓角卡片式的介面裡顯得突兀。想讓高亮跟著外框走圓角,做法是攔截觸碰事件,依點擊位置換上對應的 selector 背景。

實作思路

關鍵在於「首項」、「中間項」、「尾項」以及「只有一項」這四種狀態,各自需要不同的圓角 shape:四個角都圓、只有上面兩角圓、只有下面兩角圓、完全沒有圓角。因此我們在 onInterceptTouchEvent 中判斷被按下的是第幾個 item,再動態設定 selector。

CornerListView.java

public class CornerListView extends ListView {

    public CornerListView(Context context) { super(context); }

    public CornerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CornerListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @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;

                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);
    }
}

四個 shape 資源

全部寫在 res/drawable 下,配色與漸層一致,只差 corners 的範圍。

app_list_corner_round.xml(四角皆圓)

<?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(僅上方兩角)

<corners android:topLeftRadius="6dip"
         android:topRightRadius="6dip"/>

app_list_corner_round_bottom.xml(僅下方兩角)

<corners android:bottomLeftRadius="6dip"
         android:bottomRightRadius="6dip"/>

app_list_corner_shape.xml(中間項,直角)

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF"
              android:endColor="#40B9FF"
              android:angle="270"/>
</shape>

使用方式

寫好之後完全不用改動既有邏輯,只要在版面檔裡把原本的 <ListView> 換成 <CornerListView>(記得帶完整套件名稱),Activity 中的程式碼一行都不用改,就能直接沿用原本的 adapter 與監聽器。

需要留意的是這種做法在每次 ACTION_DOWN 時都會呼叫 setSelector,若列表很長且 frequently 觸發,可以考慮加上判斷避免重複設定。另外若要支援不同圓角半徑,把 6dip 抽成 dimens 資源會更好維護。

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