編輯: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>
接著打開 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,形成標題被「擠壓」上移的效果。
只改動了以上兩處,其他文件保持不變,直接運行。效果相當不錯:手指在右側字母表上滑動時,通訊錄列表隨之跳動,並在屏幕中央彈出一個當前分組的提示。
至此,分組導航、擠壓動畫、字母表快速滾動——系統通訊錄的全套特效我們都親手實現了一遍,是不是很有成就感?
UiAutomator 自動化測試:UiObject.getFromParent() 用法解析
方法定位 從名字就能猜到 UiObject.getFromParent() 的作用:從當前對象的父容器中查找目標子對象。前提條件是——這個子對象與當前對象處於同一層級。
Android 本地化實作:依語言環境切換字串資源
當 App 要面對不同語言的市場時,本地化是必要步驟。Android 透過資源資料夾的命名規則,讓系統自動選擇最適合的語言資源。 字串資源 預設字串放在 res/valu
Android 多語言本地化完整指南
一款 Android 應用往往會在不同國家、各種裝置上運行,使用者來自各地。要讓應用更具親和力,就必須針對當前語言環境妥善處理文字、數字、檔案等內容,這正是本地化的核心目
三分鐘做出 Android 登入頁與失敗次數限制
登入畫面是所有需要帳號的 App 的第一道門。它看起來簡單,實際上要顧及的地方不少:密碼不能被看見、錯誤要有清楚提示、以及最重要的——不能讓人無限次猜下去。 本文用最精