Android xlistview supports drop-down refresh and click up to load more custom recyclerviews

First of all, let's show you the renderings. It feels good. Please continue to read:

Drop down refresh:

Up stroke loading

In the process of updating the project, there is a need to replace xlistview with recyclerview, and the general effect cannot change after the replacement. However, for the effect of drop-down refresh, Google's solution is to put recyclerview in a swiperefreshlayout, but this is actually realized by pulling down a small circular control, The header effect is different from that of xlistview. I found a lot of other people's code on the Internet, but it didn't achieve the effect I wanted, so I wrote one myself.

The specific effects are as follows

Pull down refresh function:

1. Achieve a flexible drag out effect: for the idea, refer to xlistview. Put a header layout at the position = 0 of recyclerview. The margin top of this layout is negative by default, so this layout is always outside the screen. When pulling down, use ontouchlistener to dynamically modify the margin top according to the movement of fingers and drag it out slowly, When the dragged distance, that is, margin top, becomes a positive number, cover the state of the header layout, change the direction of the arrow and change the prompt

2. Achieve elastic rebound effect: write an animation of margin top of dynamically modified header layout with TimerTask, reduce the value of margin top every certain time, and execute the animation through a function updateheaderheight() when the user lets go.

3. Realize the automatic refresh effect of users' non manual dragging: another method of this recyclerview is called forcerefresh (), that is, the user does not need to manually pull down, the head rolls out, and then takes it back after refreshing. The automatic pull-down also uses a TimerTask to increase the value of margin top every ten milliseconds to make the head slowly exposed

Click up to load more functions:

1. Realize the effect of automatic stop when scrolling to the bottom: sometimes the recyclerview scrolls too fast, and it will float upward according to the inertia when rolling to the bottom. When this place reaches the bottom, the scrolling speed of recyclerview is monitored. If it is very fast, it means inertial scrolling, and the height of the footer layout is not modified

2. Achieve the effect of dragging upward: copy the onscrolllistener of recyclerview. When the finger rolls upward, dynamically modify the margin bottom of the bottom footerview through the updatefooterheight() method. Like the headerview, make the margin larger when the finger moves to increase the height of the footer layout, and the finger moves across the Internet, The smaller the height of the margin is, the more flexible the pull-up effect is to prevent misoperation.

3. Achieve the effect of automatic rebound: determine whether to start refreshing when you let go by monitoring the margin bottom of the footer layout. If the margin bottom is greater than a certain value, modify the status of the footer layout from normal to ready, and start refreshing when you let go in the ready state, Like the header layout, the rebound effect is achieved by modifying the size of the margin every ten milliseconds through a TimerTask

matters needing attention:

1. In order to separate the header from the bottom, the header uses ontouchlistener and the bottom uses onscrolllistener

2. A layoutmanager has been built in the recyclerview, so do not set the layoutmanager for the recyclerview. Otherwise, the header will not come out, the drop-down will report a null pointer, and the bottom will appear, but sliding has no effect

3. This recyclerview has a built-in abstract class as an adapter. Please inherit the built-in alxdragrecyclerviewadapter for use, or rewrite the adapter according to the logic

If you have any other questions, please ask me

4. For some common functions, such as setting whether the control can support pull-down loading and pull-up refresh, and other API interfaces, please refer to the usage of xlistview directly

usage method:

Inherit AlxDragRecyclerViewAdapter to write a adapter, then write two classes to implement OnRefreshListener and LoadMoreListener respectively, and write the specific refresh logic in it. After notifyDataSetChanged is ready to display data, the notifyDataSetChanged () method or notifyItemInserted () method of adapter is invoked and the stopLoadmore () method and stopRefresh (recyclerView) of the recyclerView are executed. method.

The following is the code. This control has many internal classes, including the head and bottom layout controls (customdragheaderview, customdragfooterview), adapter and layoutmanager, which have been integrated in the form of internal classes to reduce the complexity of migration

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>