Android recyclerview data binding instance code

preface

In the last project, there were a lot of recyclerviews, and then I needed to write a lot of adapters and viewholders - no problem, but there were a lot of duplicate code, which was unbearable! Each adapter and viewholder actually do very similar things: View binding, data binding, click event distribution. What else? Since they do the same thing, why should we continue to write repetitive code?

text

BaseAdapter

Usually we create a recyclerview How does the adapter do it?

It's basically this routine, or add a refreshdata () method -- pass in new data, and then notifydatasetchanged (). Based on these points, I wrote a baseadapter base class:

Its subclasses need to specify the specific type of generic type when inheriting it, because different items may have different data types, so they can adapt to more items. In addition, an interface onitemclicklistener is mentioned, which is very simple:

You also need to use generics when using it -- for the same reason as above.

Through the baseadapter above, we have encapsulated many common operations in the base class, and its subclasses only need to create different viewholders as needed -- of course, this viewholder must inherit from baseviewholder, and what baseviewholder is will be explained in detail below. The following is an example. Suppose we now have a recyclerview in an interface, and the data of each item is a string value. How can we use our baseadapter to simplify the development process?

Yes, you are right! Just a few lines of code! Five seconds! Surprise?!

You only need to create a sampleadapter, inherit from baseadapter, specify its generic type as string, and then return new sampleviewholder (context, parent) to complete the whole operation.

However, some readers may wonder: maybe we need to do a lot of operations in sampleviewholder? Isn't that just converting the code to a place, and there is no optimization in essence.

However, it is not.

BaseViewHolder

I always think it is unwise to write viewholder in adapter. In this case, the responsibility of the adapter class is too heavy. It does too much. It has almost done everything from data receiving to interface binding, from control initialization to click event distribution. And this is very bad. Easily, the code of a complex recyclerview adapter can reach hundreds of lines, which is terrible, which means that this class will become cumbersome and difficult to maintain. So how to avoid this? I chose to separate the viewholder and increase the responsibilities of the viewholder to make them more balanced.

Look directly at the code of baseviewholder:

Baseviewholder also uses generics to adapt to different data types. At the same time, I used butterknife in baseviewholder to simplify the code, so I don't have to use findviewbyid again and again.

In addition, you can see that three parameters are passed in the construction method of baseviewholder, but in the example of baseadapter above, we only passed in the first two construction parameters of sampleviewholder, and the third parameter layoutres is not passed in. What's the matter? Is it a mistake? Of course not. The baseviewholder constructor has three arguments because it requires three arguments, while its subclass has only two arguments because it can only have two arguments_ Baseviewholder must satisfy its super structure, so it must have those three parameters, and its subclass. If those three parameters are passed in from the outside, how can it perform specific operations for that layout? It must explicitly specify the layout ID in the class In fact, this is a place I really want to optimize, because in this case, the subclass needs to modify its construction method after inheriting baseviewholder, which is more worrying, but there is no good idea to optimize it gracefully.

There are two similar methods in baseviewholder: SetData () and binddata (). However, in fact, they are completely different except for the same parameters. SetData () method is a public method, which can be called by the object of the subclass of baseviewholder. Its function is to transfer data from the outside for the initialization of the view held by the viewholder. It can be said to be a bridge for transmitting information; GetData () is an abstract method. Its concrete implementation is in each subclass of baseviewholder. These subclasses bind and initialize the control, and handle the click event of the control in this method.

When it comes to the handling of click events, how should its subclasses accomplish this? Through onitemclicklistener. We can use the interface callback of onitemclicklistener to pass the click events to be processed to the outside world, and then the outside world can process them. So what if there are click events of multiple controls that need to be handled? Don't worry, because in the onclick method of onitemclicklistener, you need to pass in the ID of the clicked control, so that you can judge the incoming ID outside, so as to execute different click events for different IDs.

Next, let's take an example to see how to use the sampleviewholder above:

It is also very simple, convenient, fast and clear. But there are still a few points worth noting. First of all, you should not forget to modify the construction method and explicitly specify the layout ID in super, otherwise you can't do it in the next step. In addition, after calling listener When using the onclick () method, the listener must be null checked -- because the listener may really be null! In this case, it is very easy to generate null exceptions and cause the program to crash.

Bingo, in this way, the sampleviewholder is completed, which is also almost effortless.

epilogue

The purpose of this blog post is to share some things I have summarized, hoping to speed up your development a little. Of course, there may be bugs I haven't found. If you find problems in the process of use, please don't be polite and hit me hard!

Finally, I'd like to summarize how to get the recyclerview set fastest with baseadapter and baseviewholder:

Viewholder related new xxxviewholder inherits from baseviewholder and specifies the generic type (that is, the data type of the data in the item). Delete the layoutres parameter in the construction method and explicitly specify the layout ID in super. Bind the control with butterknife. Complete the initialization of the control and the transmission of click events in the binddata () method (don't forget the blank check of the listener)

Adapter related

The new xxxadapter inherits from baseadapter and specifies the generic type (that is, the data type of the data in the item). return new XXXViewHolder(context,parent);

External correlation

Bind recyclerview and create a new xxxadapter. Call baseadapter The refreshdata () method passes in the data list. If there is a requirement for click event processing, baseadapter.exe is called Setonclicklistener() method.

At present, I have only made baseadapter for a single item in recyclerview, but baseviewholder can be universal, and it can greatly simplify the volume of adapter under multiple items.

The above is the data sorting of Android recyclerview data binding. Continue to supplement relevant data in the future. Thank you for your support for this site!

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