OnScrollListener接口官方定義是:Interface definition for a callback to be invoked when the list or grid has been scrolled.翻譯過來大意是指:當列表或者網格(AbsListView抽象類的實現子類,一般應該是指ListView類和GridView類及其子類)產生了滾動行爲時,將會回調該接口。該接口是AbsListView類內部的一個靜態類。所以,一般的控件比如button,textView等並不能添加該接口監聽。
滾動行爲有三種狀態:
1. SCROLL_STATE_FLING : The user had previously been scrolling using touch and had performed a fling.可理解爲手指觸摸屏幕滑動了一下,鬆開手後由於慣性view仍會滑動一段距離,這一過程即位Fling(猛衝)。對應的整形常量值爲2。
2. SCROLL_STATE_IDLE : The view is not scrolling.指view不再滾動。對應的整形常量值爲0。
3. SCROLL_STATE_TOUCH_SCROLL :The user is scrolling using touch, and their finger is still on the screen.指按着屏幕滑動時的狀態。對應的整形常量值爲1。
該接口有兩個抽象方法分別爲:onScrollStateChanged(),和onScroll()
其中:void onScrollStateChanged(AbsListView view, int scrollState)會在滾動狀態發生改變時回調,比如從滑動狀態到停止下來時就會回調該方法。它的兩個參數:view表示添加了該接口監聽的組件,scrollState表示當前的滾動狀態。
void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
- Callback method to be invoked when the list or grid has been scrolled. This will be called after the scroll has completed
-
- 參數:
view
- The view whose scroll state is being reportedfirstVisibleItem
- the index of the first visible cell (ignore if visibleItemCount == 0)指view中可看到的第一項所對應的索引值。visibleItemCount
- the number of visible cells指整個view中可看到的條目的數量。totalItemCount
- the number of items in the list adaptor。指整個view中所包含的總的條目的數量。