How does Android use recyclerview to create a home page rotation map

First look at the renderings:

Stop in the middle and turn the page automatically

Preface: I recently received a task to do a function similar to automatic page turning above. It can be seen that three pictures are displayed on this screen, and two are not fully displayed. When you see the design drawing, the first reaction is that you can use viewpager, but the task makes a big joke on you. It is required to take the leftmost picture as the benchmark, that is, the picture on the left must also be fully displayed, as shown in the figure below, Later, when you think about it carefully, viewpager doesn't seem to have such a function. Maybe I don't know, and I haven't found such an article or information. I'd like to have a private exchange with Jian you. Thank you very much. OK, back to business

Stop on the left

Before we start, we will first introduce the snaphelper, the latest thing released by Google (in fact, it was released in version 24.2.0). This thing is an extension of the function of recyclerview. Interested students can see its source code. The implementation principle of snaphelper is to listen to the onfling interface in recyclerview.onflinglistener, Recyclerview can realize the function similar to viewpager. No matter how it slides, it finally stays in the middle of a page. What is the difference between recyclerview and viewpager? That is, the viewpager cannot slide multiple pictures continuously at one time, and cannot be customized (stop on the left or right). Let's have a look!

First, import the required package. The minimum version is v7-24.2.0. If it is lower, there will be no such class:

compile 'com.android.support:appcompat-v7:24.2.0' compile 'com.android.support:recyclerview-v7:24.2.0'

The system comes with a class linearsnaphelper, which inherits from snaphelper. By default, the view is stopped in the middle. You only need to bind recyclerview and linearsnaphelper together:

LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper(); mLinearSnapHelper.attachToRecyclerView(mRecyclerview);

The effects are as follows:

Of course, the function of snaphelper is not just here. You can also customize it to stop on the left or right. You don't need to inherit snaphelper again, just inherit linearsnaphelper directly. There are many written methods, and then you can rewrite the two methods: * (1) Calculatedistancetofinalsnap: this method will be called back when dragging or sliding is completed, and an out = int [2], out [0] x axis and out [1] y axis will be returned. This value is the offset of the position you need to correct. ** (2) . findsnapview: this method is used to obtain a specific view. When null is returned, it means that no view has been obtained*

Complete code:

Of course, you can also stop it on the right: just modify the findsnapview method based on the above:

effect:

Stop on the right

How to make it slide infinitely?

Of course, this is a "trick" in the adapter. Let the method to get the total number of items return integer.max_ Value is OK:

Then, when the value in the list is obtained in onbindviewholder, the corresponding remainder is good:

Well, 80% is done here. Next, we need to make it scroll automatically. How can it scroll automatically? Here, you can refer to the effect of automatic scrolling in viewpager. LZ uses timer to implement it. Timer has a function of how often to execute it. Here it happens:

Considering that there are two kinds of sliding, one is the user's manual sliding, and the other is our timer to start sliding, we also have to set monitoring on recyclerview to simply judge whether it is triggered by the user or timer:

Of course, the final processing result is handed over to the handler:

That's almost good, but there's another problem. I don't know if you've noticed that the rolling is not so smooth. I haven't known the reason for looking for it for a long time. I hope to let you know in the comments below.

Reference article: learning and using snaphelper in Android 24.2.0 support library of orientationhelper API

Thank you for your help.

I have uploaded the source code to GitHub. Friends who want to know can download it by themselves. Of course, if they have better ideas, they can also communicate together.

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