Android uses custom recyclerview to achieve Gallery effect

1. Basic usage of recyclerview

First, the layout file of the main activity:

Layout file of item:

Data adapter:

You can see that the data adapter has undergone considerable changes compared with baseadapter. There are three main methods:

Getitemcount needless to say, get the total number of entries oncreateviewholder, create viewholder onbindviewholder, and bind the data to the viewholder

It can be seen that recyclerview also encapsulates the viewholder to some extent, but if you observe carefully, you will ask a question. There is a getview in the listview that returns the view as an item layout. Where is the appearance of this item controlled?

In fact, the viewholder we create must inherit recyclerview.viewholder. When constructing this recyclerview.viewholder, we must pass in a view, which is equivalent to the convertview in our listview getview (that is, we need to pass in the item layout of inflate).

In addition, convertview in listview is reused. In recyclerview, viewholder is used as the cache unit, and then convertview is maintained in viewholder as the member variable of viewholder. That is to say, assuming that 10 items are not displayed on the screen, 10 viewholders will be created for caching, and viewholders will be reused each time, So he changed the getview method to oncreateviewholder. If you are interested, print the log and test it.

Finally, use in activity:

It is also very convenient to use. The only difference is to set layoutmanager. At present, there is only one implementation class, linearlayoutmanager, which can be set to horizontal or vertical.

Final rendering:

The effect is very good. This is the basic usage of recyclerview, but you will find a place where there is no callback setonitemclicklistener. Do you want to be so stupid...

2. Add onitemclicklistener callback for recyclerview

Although it is not provided, adding an onitemclicklistener is not a piece of cake for us~

I decided to add this callback interface to the adapter:

Very simple, create an interface, provide a setting entry, and then judge in onbindviewholder.

Finally, set listening in the main activity:

OK, that's it. Look at the renderings:

The effect is still good. Next, I want to change it to album effect, that is, a large picture is displayed on the top, and the recyclerview below is used as an indicator for picture switching.

3. Customize recyclerview to realize content linkage during scrolling

First modify the following layout:

Layout file:

Add an area to display a large picture, and change the recyclerview to its own definition.

Then look at our custom recyclerview Code:

Define an interface for callback during scrolling, and then listen for action in ontouchevent_ Move: when the user's finger slides, the user will constantly call back the current first view~

How do I know that getchildat (0) and getchildposition () are available? At first, I thought there was a getfirstvisibleitem method, but later I found that there was none; However, I found getrecycledviewpool (). According to the name, I think it is the cache queue of viewholder. I think the first one in this queue is the view I want. Later, it didn't succeed. I looked at its internal view, and finally found that the first displayed is always its first child. As for getchildposition, I can see it.

Effect now:

The result is as like as two peas in my previous example, but I still want to make some changes. I think Gallery or the indicator of album, maybe 1000 pictures below, I love the picture automatically when I move my fingers on the screen. I also hope that if I give the indicator an acceleration, even if the finger leaves and the lower part is still sliding, the upper part will be linked. And I want to do some optimization directly in action_ The callback in move is triggered too frequently. Theoretically, a picture will only be triggered once~~

4. Optimize and create real Gallery effect

Since you want your fingers to move away, you need not only action_ Move needs to monitor, but also to monitor an acceleration, the speed reaches a certain value, and then continue to move ~ ~ do it again. Do you need such trouble? Can you roll? Then there should be onscrolllistener. Look down. Sure enough, ha ha ~ God help me too. Here is the modified Code:

Instead of rewriting the ontouchevent method, I let this class implement the recyclerview.onscrolllistener interface, and then set the listener to judge in onscroled.

As for optimization: I use a member change to store the current first view, and only call back when the first view changes ~ ~ perfect~

See mainactivity:

The code hasn't changed much ~ there are multiple setting callbacks~

design sketch:

You can see that it not only supports the changes when the mobile phone moves on it, but if I give an acceleration, the following continues to scroll, and the above will also continue to change ~ ~ great praise ~ callback once for each picture, and the efficiency is also quite good.

Well, after reading the blog here, I believe you have a certain understanding of recyclerview, and even know more about how to transform a control~~

If you think it's good, leave a message or praise to show your support for me

Click to download the source code

summary

The above is what Xiaobian introduced to you. Android uses a custom recyclerview to achieve the gallery effect. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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