Android uses recyclerview to implement horizontal scroll control
preface
I believe everyone knows that there are many ways to implement Android scroll control, and it is relatively simple to use recyclerview. We have made a simple age scroll control. Let's take a look at the usage of recyclerview, mainly including the following points:
(1) Align the center of the control
(2) Calculate the rolling distance
(3) Highlight the center view
(4) Real time display of central data
(5) Automatic alignment when stopped
(6) When scrolling, set the button status switch
effect
1. Framework
It mainly focuses on the logic of recyclerview
Set a horizontal layout
Add an adapter and set the start and end positions
2. Adapter
Adapter customizes some functions, such as pointing to the center, data linkage display, highlighting item, etc
The width of each cell is one odd part of the screen width. When the screen is filled, the starting point points to the center
If the number of items is singular, the indicator of the center must point to the center of the central item
Update the item style according to the selected status
Set the highlight, highlight the center position, restore both sides, and notify the adapter to redraw the viewholder
Get the width. If the number of items displayed in a row is set to an odd number, the center points to an item
Note that recyclerview cannot move half a cell. If the number of each row is singular, it must point to the center
3. Scroll logic
When scrolling, the page display is updated in real time; When stopped, update the highlighted and stored data; At the end of scrolling, activate the button
Judge the distance unit of scrolling, offset total distance / single item width
Computehorizontalscrolloffset() gets the total offset of the recyclerview
In the middle position, add the number of items in half a line
Judge whether the scrolling stops
Automatic alignment when scrolling stops
Call the item that needs to be highlighted
Note that when scrolling stops, it is better to update the highlight, otherwise the redrawing speed is slow, which will affect the scrolling effect
Sliding effect
summary
Well, that's all the content of this article. We can customize all kinds of scroll bars according to these! I hope this article can help you.