Android recyclerview scroll positioning

Overview recyclerview is very practical and easy to use in Android development, but there has always been a problem bothering me in actual development, that is, positioning. In actual projects, there are always such needs: retrieve an item of recyclerview (the height of each item is uncertain), then locate this item and display it on the top. This cannot be achieved by using the default move method of recyclerview (personally, I think the official may not achieve this for performance reasons). This blog will explain how I personally realize this demand.

Demo demo

Thinking before typing code

There are two methods for controlling movement provided by recyclerview - scrolltoposition (int). The function of this method is to display the specified item, that is, to display the item you want to top, but it doesn't matter where it is on the screen. As long as that item can be seen now, it will strike- Scrollby (int x, int y) this method controls the moving distance by itself. The unit should be pixels.

When using scrolltoposition, when moving to the front item, it will set the item to be displayed to the top by default, but when moving to the rear item, the position is not good (see its mood!) and is generally displayed in the last item. You should know everything you've used. When using scrollby, you need to calculate the height or width yourself. In dynamic layout, it is very difficult to calculate the height by yourself when the height of each style may be different.

After all the nonsense mentioned above, the conclusion is that neither of these two methods can solve the problem well. However, when they are used together, our problems become much easier to solve!

The idea is: first use scrolltoposition to move and display the item to be placed on the top, then calculate the distance from the item to the top, and use scrollby to complete the last 100 meters!

critical code

First pass in the top items, and then handle them differently

Recyclerview scrolling monitor

Demo download

[CSDN]: http://xiazai.jb51.net/201701/yuanma/RecyclerViewLocation (jb51.net).rar

[GitHub]: https://github.com/tyzlmjj/AndroidUI/tree/master/RecyclerViewLocation

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>