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

Android 仿微信聯絡人字母排序效果實作

編輯:關於 Android 程式設計

效果說明

聯絡人列表常見字母索引功能,使用者可以透過右側字母列快速跳到對應姓氏或名稱。這種效果在通訊錄、好友列表與音樂應用中都很常見。本文以類似微信聯絡人的字母索引為例,說明實作思路。

版面結構

整體版面可以使用 RelativeLayout,包含:

1. ListView:顯示聯絡人資料。
2. 自訂 View:右側字母索引列。
3. TextView:中央提示方塊,顯示目前點擊的字母。

<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.example.view.QuickIndexBar
        android:id="@+id/quickIndexBar"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@+id/tvHint"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textSize="24sp"
        android:visibility="gone" />
</RelativeLayout>

字母索引自訂 View

右側字母列可以自訂 View 實作。繪製時,先取得 View 高度,再除以字母數量,即可得到每個字母的高度。由於字母寬度通常固定,可以直接設定一個合適的寬度。

public class QuickIndexBar extends View {
    private Paint paint;
    private String[] letters = {
        "A", "B", "C", "D", "E", "F", "G", "H",
        "I", "J", "K", "L", "M", "N", "O", "P",
        "Q", "R", "S", "T", "U", "V", "W", "X",
        "Y", "Z"
    };

    private int cellHeight;

    public QuickIndexBar(Context context) {
        super(context);
        paint = new Paint();
        paint.setTextSize(20);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onSizeChanged(
        int w, int h, int oldW, int oldH) {
        super.onSizeChanged(w, h, oldW, oldH);
        cellHeight = h / letters.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < letters.length; i++) {
            float x = getWidth() / 2f
                - paint.measureText(letters[i]) / 2f;
            float y = cellHeight * i + cellHeight / 2f;
            canvas.drawText(letters[i], x, y, paint);
        }
    }
}

為了讓字母位置更精準,實際繪製時常需要計算文字基線,避免字母看起來偏移。

觸摸事件與回呼

當使用者點擊或滑動右側字母列時,可以根據觸摸 Y 座標除以每格高度,計算目前對應的字母。

@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    int index = (int) (y / cellHeight);

    if (index >= 0 && index < letters.length) {
        if (listener != null) {
            listener.onLetterChange(letters[index]);
        }
    }
    return true;
}

接著在 Activity 中,當收到字母變更時,讓 ListView 跳到第一個符合該字母的項目。

中文轉拼音

若聯絡人名稱是中文,通常需要先轉成拼音,再取首字母。常見做法是使用 pinyin4j 或類似套件。資料模型可以保存名稱與拼音:

public class Contact {
    public String name;
    public String pinyin;

    public Contact(String name, String pinyin) {
        this.name = name;
        this.pinyin = pinyin;
    }
}

在 Adapter 中,可以比較目前項目與上一個項目的拼音首字母。若相同,隱藏左側字母標題;若不同,顯示該字母,形成分組效果。

中央提示效果

當使用者觸摸字母列時,可以在畫面中央顯示一個提示方塊,顯示目前字母。手指離開後再隱藏。這個效果能提升操作回饋,讓使用者知道目前選到哪個索引。

結語

仿微信聯絡人字母排序的重點在於:自訂 View 繪製字母、計算觸摸位置、將中文轉為拼音、並讓 ListView 依字母定位。理解這些步驟後,就能延伸實作出更完整的聯絡人索引體驗。

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