Android listview method for monitoring sliding events (detailed explanation)
Listview mainly has two sliding event listening methods, ontouchlistener and onscrolllistener
1、OnTouchListener
The ontouchlistener method comes from the listening events in view. When listening to three action events, you can obtain the coordinate value of the current touch through the getx() method or gety() method of motionevent to judge the sliding direction of the user, and make corresponding processing in different action states
Not only the above three action states, but also many other states are defined in the motionevent class. We can use these states flexibly
• MotionEvent.ACTION_ Down: start touch
• MotionEvent.ACTION_ Move: touch move
• MotionEvent.ACTION_ Up: touch lift
• MotionEvent.ACTION_ Outside: the touch range exceeds the UI boundary
• MotionEvent.ACTION_ Cancel: when the touch is cancelled
• MotionEvent.ACTION_ POINTER_ Down: when another touch is pressed (multi touch)
• MotionEvent.ACTION_ POINTER_ Up: when another touch is raised (multi touch)
2、OnScrollListener
Onscrolllistener comes from the listening events in abslistview. Because listview directly inherits from abslistview, there are many listview related information in abslistview
There are two callback methods in onscrolllistener
• public void onscrollstate changed (abslistview, int scrollstate): monitor the change of sliding state
• public void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount): listen for sliding
There is a detailed explanation in the source code
2.1 onscrollsatechanged method
Onscrollsatechanged determines the number of callbacks based on the scrollstate. It has three modes:
• OnScrollListener.SCROLL_ STATE_ Idle: the state when scrolling stops
• OnScrollListener.SCROLL_ STATE_ STOUCH_ Scroll: the state when the touch is scrolling and the finger has not left the interface
• OnScrollListener.SCROLL_ STATE_ Fling: the state when the listview will continue to slide due to inertia after the user slides with force
When the user does not slide with force, the onscrollsatechanged method will only call back twice, otherwise it will call back three times. When we use it, we usually set the flag flag to distinguish different sliding States, so as to carry out corresponding processing
2.2 onscroll method
When the listview scrolls, it will always be called back. It displays the scrolling status of the current listview through three parameters
• firstvisibleitem: ID of the first item currently visible (starting from 0)
• visibleitemcount: the total number of currently visible items
• totalitemcount: the total number of adapters in the list, that is, the total number of items in the whole listview
Note: the total number of items currently visible, including items that are not fully displayed on the screen. If half of the items are displayed, they will also be included in the visible range
Through these three parameters, we can judge many events, such as:
(1) Determines whether to slide to the last line
If the ID of the first item in the current view plus the total number of visible items in the current screen is equal to the total number of all items in the listview, it indicates that it has moved to the last line
(2) Judge the sliding direction
Record the position of the last firstvisibleitem through the oldvisibleitem, and then compare it with the first visibleitem after sliding to know the sliding direction
The above Android listview method for monitoring sliding events (detailed explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.