Implementation of recyclerview general adapter in Android

Implementation of recyclerview general adapter in Android

preface:

Version 5.0 of the SDK has been out for N years. It can be said that it has been tested by many people. The new controls inside can not be said to be perfect, but they are also very easy to use. One of my favorite is recyclerview, which can perfectly replace listview and GridView (in addition to adding headview and footview, there are many solutions on the Internet. This will be solved in a simple way below, which will certainly save you worry). In addition, you can dynamically switch these two layout methods and waterfall flow layout by code. There are many related switching methods on the Internet. You can search by yourself, so I won't post the connection.

I believe that when you used listview before, you must have been tired of repeatedly writing countless adapters. Of course, some smart people like me must have been using universal adapters. However, recyclerview requires that we must use viewholder to implement the adapter. This makes many people who are used to universal adapters unhappy. Today, I will provide the general adapter of recyclerview, which has been improved and implemented based on the implementation principle of listview universal adapter. Since it is not teaching and cutting the code is relatively simple, it will not be explained in sections. I believe you can understand it by reading the notes.

There are two classes in total. One inherits the rviewholder class implemented by android.support.v7.widget.recyclerview.viewholder of the system, which implements the cache of any control, and the other inherits the rbaseaadapter class implemented by android.support.v7.widget.recyclerview.adapter.

Implementation code:

RViewHolder

This is rbaseaadapter

For the rbaseadapter, the first is generic, so that any object type can be used. The second is the solution of headview and footview mentioned earlier. You can see that rbaseadapter defines several methods to add headview and footview by loading different layouts through different positions.

Called in activity

Thank you for reading, hope to help you, thank you for your support to 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
分享
二维码
< <上一篇
下一篇>>