編輯:關於 Android 程式設計
做分頁載入時,判斷「有沒有滑到底」決定要不要抓下一頁;做下拉刷新時,判斷「有沒有回到頂端」決定要不要顯示刷新提示。這兩個判斷的公式其實不難,但很容易用錯 View 的方法。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="2000dip"
android:text="text" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ScrollView view = (ScrollView) findViewById(R.id.scroll_view);
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (v.getScrollY() <= 0) {
Log.d("scroll view", "top");
} else if (view.getChildAt(0).getMeasuredHeight()
<= v.getHeight() + v.getScrollY()) {
Log.d("scroll view", "bottom");
Log.d("scroll view",
"view.getMeasuredHeight() = " + view.getMeasuredHeight()
+ ", v.getHeight() = " + v.getHeight()
+ ", v.getScrollY() = " + v.getScrollY()
+ ", child.getMeasuredHeight() = "
+ view.getChildAt(0).getMeasuredHeight());
}
break;
default:
break;
}
return false; // 不回傳 true,不干擾 ScrollView 自身的捲動
}
});
}
}
這段邏輯最容易搞錯的就是這三個方法:
實際 log 出來的數值大概是這樣:
view.getMeasuredHeight() = 1038 v.getHeight() = 1038 v.getScrollY() = 2962 view.getChildAt(0).getMeasuredHeight() = 4000
驗算一下:4000 - 1038 = 2962,正好等於捲到底時的 scrollY。所以判斷式 childHeight <= viewHeight + scrollY 成立時,就是已經到底。
這是最關鍵的陷阱。ScrollView 的 getMeasuredHeight() 回傳的是它自己的高度(1038),而不是內容的高度。用它來跟 scrollY + height 比較,等號會在捲動一開始就成立,於是「到底」的訊息被瘋狂觸發。
內容的高度必須從它的直接子 View 取得。這是因為 ScrollView 只允許一個 child,那個 child 的 measured height 就是可捲動的總長度。
onTouch 回傳 false,代表「我處理不完,請繼續往下傳」。ScrollView 自身的捲動邏輯依賴接收完整的觸摸事件序列,若回傳 true,事件就被攔截,畫面會變得無法滑動。這裡只是「旁聽」事件做判斷,所以必須回 false。
用 OnTouchListener 做檢測有幾個缺點:ACTION_MOVE 會在滑動過程中觸發非常多次,容易產生大量 log 與不必要的判斷;而且它無法涵蓋「用程式呼叫 scrollTo() 造成的位置變化」。
繼承 ScrollView 並覆寫 onScrollChanged(),它在每次捲動位置改變時被呼叫,比 onTouch 精準且觸發次數合理:
public class ObservableScrollView extends ScrollView {
public interface OnScrollListener {
void onScrollChanged(int x, int y, int oldX, int oldY);
}
private OnScrollListener listener;
public void setOnScrollListener(OnScrollListener l) {
this.listener = l;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (listener != null) {
listener.onScrollChanged(x, y, oldX, oldY);
}
}
public boolean isAtTop() {
return getScrollY() == 0;
}
public boolean isAtBottom() {
View child = getChildAt(0);
return child != null
&& child.getMeasuredHeight() <= getHeight() + getScrollY();
}
}
API 21 之後的 NestedScrollView 支援嵌套捲動,並提供 setOnScrollChangeListener(),不必自己繼承:
nestedScrollView.setOnScrollChangeListener(
(NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY,
oldScrollX, oldScrollY) -> {
View child = v.getChildAt(v.getChildCount() - 1);
if (scrollY == 0) {
// 到頂
} else if (child != null
&& child.getMeasuredHeight() <= v.getHeight() + scrollY) {
// 到底
}
});
若清單本身就是列表,直接用 RecyclerView 更合適:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView rv, int dx, int dy) {
GridLayoutManager lm = (GridLayoutManager) rv.getLayoutManager();
int total = lm.getItemCount();
int last = lm.findLastVisibleItemPosition();
if (last >= 0 && last == total - 1) {
// 到底了,載入下一頁
}
}
});
實務上通常還會加上「距離底部多少」的門檻(例如剩 5 項時就預先載入),讓使用者感覺不到等待。
Android 輸入管理系統機制解析
Android 的輸入管理系統主要負責按鍵、觸摸板、鼠標等輸入設備的事件輸入,功能涵蓋:輸入設備的事件讀取,以及向焦點窗口與焦點視圖的事件派發,事件的插入、過濾與攔截等。
深入剖析 Android Service 的綁定全流程
在我們日常的 Android 開發中,只要涉及 Service 的雙向通信,就離不開「綁定」這個動作。唯有當 Service 處於已綁定狀態時,客戶端才能拿到一個 Bin
Android 自定義日曆控件:內容樣式皆可擴展的精美日曆
需求背景 Android 系統本身內建了日曆控件,網路上也有不少開源的日曆資源,但這些控件往往樣式單一、API 繁多,不易在實際專案中擴展,也难以做出符合特定風格、內容可
Android Scroller 滾動機制完全解析
Android 的 View 本身就具備滾動能力,核心方法是 scrollTo 與 scrollBy。若想做出更順滑、可控的滾動動畫,則需要搭配 Scroller 與 c