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

打造媲美系統通訊錄的 A-Z 字母表快速滾動

編輯:Android 開發實例

在上一篇文章裡,我們一起實現了類似 Android 系統通訊錄的分組導航與擠壓動畫效果。本文將在原有代碼基礎上繼續改進,加入更實用的「A-Z 字母表快速滾動」功能。若還沒看過上一篇,建議先補讀,銜接會更順暢。

為什麼不用系統自帶的快速滾動

其實 ListView 本身自帶快速滾動能力,只要在 XML 裡設置 android:fastScrollEnabled="true" 即可啟用,早期版本的系統通訊錄就是用這種方式。但它的滑塊樣式相對單調,後來不少手機廠商在定制 ROM 時,都把默認快速滾動換成了類似 iPhone 的 A-Z 字母表方案。既然要追潮流,我們也毫不客氣地採用字母表方案。

改造布局文件

打開上次的 ContactsDemo 工程,修改 activity_main.xml。由於要在界面上加入字母表,我們需要一個 Button,把它的背景設為一張 A-Z 排序的圖片,並靠右對齊;另外再加一個 TextView,用於在彈出式分組上顯示當前字母,默認 gone,只有手指在字母表上滑動時才顯示。修改後的布局如下:

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

    <ListView
        android:id="@+id/contacts_list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scrollbars="none"
        android:fadingEdge="none" >
    </ListView>

    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="fill_parent"
        android:layout_height="18dip"
        android:layout_alignParentTop="true"
        android:background="#303030" >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dip"
            android:textColor="#ffffff"
            android:textSize="13sp" />
    </LinearLayout>

    <Button
        android:id="@+id/alphabetButton"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:background="@drawable/a_z" />

    <RelativeLayout
        android:id="@+id/section_toast_layout"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:layout_centerInParent="true"
        android:background="@drawable/section_toast"
        android:visibility="gone" >
        <TextView
            android:id="@+id/section_toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#fff"
            android:textSize="30sp" />
    </RelativeLayout>

</RelativeLayout>

核心邏輯:監聽字母表的 touch 事件

接著打開 MainActivity,我們需要監聽字母表按鈕的 touch 事件。核心代碼如下:

private void setAlpabetListener() {
    alphabetButton.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float alphabetHeight = alphabetButton.getHeight();
            float y = event.getY();
            int sectionPosition = (int) ((y / alphabetHeight) / (1f / 27f));
            if (sectionPosition < 0) {
                sectionPosition = 0;
            } else if (sectionPosition > 26) {
                sectionPosition = 26;
            }
            String sectionLetter = String.valueOf(alphabet.charAt(sectionPosition));
            int position = indexer.getPositionForSection(sectionPosition);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    alphabetButton.setBackgroundResource(R.drawable.a_z_click);
                    sectionToastLayout.setVisibility(View.VISIBLE);
                    sectionToastText.setText(sectionLetter);
                    contactsListView.setSelection(position);
                    break;
                case MotionEvent.ACTION_MOVE:
                    sectionToastText.setText(sectionLetter);
                    contactsListView.setSelection(position);
                    break;
                default:
                    alphabetButton.setBackgroundResource(R.drawable.a_z);
                    sectionToastLayout.setVisibility(View.GONE);
            }
            return true;
        }
    });
}

逐行理解這段邏輯:先用 getHeight 拿到字母表總高度,再用 event.getY 取得手指的縱坐標,用縱坐標除以總高度,就得到一個 0 到 1 之間的小數(0 對應 # 端,1 對應 Z 端)。由於字母表共有 27 個字符,把這個小數再除以 1/27 並向下取整,就能算出當前按在哪個字母上。隨後判斷 action:若是 ACTION_DOWN 或 ACTION_MOVE,就在彈出式分組上顯示當前字母,並調用 ListView 的 setSelection 把列表滾動到對應分組;若是其他 action,則隱藏彈出式分組。

MainActivity 的完整結構大致如下(保留關鍵字段與方法):

public class MainActivity extends Activity {

    private LinearLayout titleLayout;          // 分組的布局
    private RelativeLayout sectionToastLayout;  // 彈出式分組的布局
    private Button alphabetButton;              // 右側可滑動字母表
    private TextView title;                     // 分組上顯示的字母
    private TextView sectionToastText;          // 彈出式分組上的文字
    private ListView contactsListView;          // 通訊錄 ListView
    private ContactAdapter adapter;             // 通訊錄列表適配器
    private AlphabetIndexer indexer;            // 用於字母表分組
    private List<Contact> contacts = new ArrayList<Contact>();
    private String alphabet = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 字母表排序規則
    private int lastFirstVisibleItem = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = new ContactAdapter(this, R.layout.contact_item, contacts);
        titleLayout = (LinearLayout) findViewById(R.id.title_layout);
        sectionToastLayout = (RelativeLayout) findViewById(R.id.section_toast_layout);
        title = (TextView) findViewById(R.id.title);
        sectionToastText = (TextView) findViewById(R.id.section_toast_text);
        alphabetButton = (Button) findViewById(R.id.alphabetButton);
        contactsListView = (ListView) findViewById(R.id.contacts_list_view);
        // 讀取系統通訊錄、構建 indexer、設置適配器……
        if (contacts.size() > 0) {
            setupContactsListView();
            setAlpabetListener();
        }
    }
    // setupContactsListView() 中通過 OnScrollListener 實現分組標題的擠壓動畫,
    // setAlpabetListener() 即上文的核心邏輯。
}

其中 setupContactsListView 沿用了上一篇的擠壓動畫邏輯:在 onScroll 回調裡,根據第一個可見 item 所屬分組更新標題文字,並在新分組頂部逼近時動態調整 topMargin,形成標題被「擠壓」上移的效果。

運行效果

只改動了以上兩處,其他文件保持不變,直接運行。效果相當不錯:手指在右側字母表上滑動時,通訊錄列表隨之跳動,並在屏幕中央彈出一個當前分組的提示。

至此,分組導航、擠壓動畫、字母表快速滾動——系統通訊錄的全套特效我們都親手實現了一遍,是不是很有成就感?

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