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

Android 自訂圓角 ListView:依首/中/尾項切換 selector

編輯:關於 Android 程式設計

預設的 ListView 每一項都是矩形,若整體設計想要圓角外框,僅對 ListView 套用背景是不夠的——按下某項時的高亮色塊會是方的,把圓角破壞掉。解法是搭配自訂 Control,依被按下的項目位置動態切換 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;
                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);
    }
}

核心概念:pointToPosition 把觸摸座標換成項目索引,再依「第一項/最後一項/中間」選擇對應的 selector 資源。

二、四個 shape 資源

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>

三、使用方式

寫好之後,在版面檔中把原本的 <ListView> 換成完整的類別名稱 com.xxx.CornerListView,其餘用法與原生 ListView 完全相同,Adapter 也不用改。

另外提醒:若要外框也有圓角,建議在 ListView 外面再包一層 CardView 或加上 android:clipToOutline,效果會比只用背景圖更乾淨。

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