Application and design principle of Android adapter mode

Adapter pattern is an important design pattern, which has been widely used in Android. The adapter is similar to the plug in the real world. Through the adapter, we can integrate two different types of data belonging to different classes without adding or modifying the methods in the class according to a certain need.

Adapters are divided into one-way adapters and two-way adapters. The former is used more frequently in Android. A common implementation method is: first define an adaptation class, internally define a private object to be adapted, provide a constructor, pass an instance of the object as a parameter, initialize it in the constructor, and then provide a public method to return the data type required by another class to be adapted. In this way, by creating an additional class, which is specially responsible for data type conversion, the required functions are realized without changing the original class. This design pattern provides better reusability and scalability, especially when we cannot modify one class or there are many different types of data between classes that need to be adapted.

Common adapter classes in Android include baseadapter, simpleadapter, etc

When I first met Android, I had a half understanding of the adapters in listview and gradview. Every time I write an adapter, I feel very painful. Therefore, with this article, I hope to help some beginners.

First, let's take a look at the official explanation of the adapter pattern, which converts the interface of a class into another interface desired by the customer. The adapter pattern enables those classes that cannot work together due to incompatible interfaces to work together. At first glance, I don't know what to say. Generally speaking, my personal understanding: after we create a view, we must then specify the size, location, displayed content information, callback of touch events, etc. we find that putting these things together leads to too coupling or even redundancy of the code. At this time, we can use the adapter mode to separate the process, First, we just create a view. As for the content style of the view, we don't care at all. We give it to our younger brother adapter (provided that this person is our younger brother and can be connected with our view). When the younger brother completes the tasks we assigned him (arranging the style of the sub view, information analysis and display, event callback, etc.), we just need to use setadapter () just steal his work.

Or more intuitively, we're going to buy an iPhone 7. We just need to use it to listen to songs, watch movies and talk about wechat. What should we do when he runs out of power? How to charge with 110V and 220V voltage? How to use two hole and three hole plugs? How to charge fast and how to charge slowly? We don't need to care about these. Just give it to our charger. All we have to do is connect the USB cable and find the socket (setadapter).

When you don't know the specific process of adaptation, it's very painful to write an adapter. Next, let's take chestnuts to analyze a complete adapter mode workflow in detail (now no one will use listview except my wonderful company, so here we directly take recyclerview as the chestnut. In fact, the adapter of listview has the same principle).

1. Create a recyclerview and instantiate it, and then wait for our adapter to complete the rest of the manual work.

2. To use the adapter, we first understand the callback cycle of each method from the source code.

Adapter is an abstract internal class of recyclerview. We only need to rewrite various callback methods exposed by it (create view, bind view, obtain data content, notify data changes...), You can create and control the contents of ItemView. Here, we will explain the rewriting methods commonly used when creating an adapter:

onCreateViewHolder:

According to the requirements, create a custom style itemviw, and finally return a viewholder type. Called by createviewholder in the adapter internal class.

onBindViewHolder:

Bind the data bean we passed in with the view, that is, determine how to display the specific content of our ItemView, which is called by bindviewholder in the internal class of the adapter. At the same time, you can also handle the callback of touch events here, which will be discussed later.

getItemCount:

Returns the number of data beans, that is, the number of itemviews required.

Generally, we can rewrite the above three methods.

onViewAttachedToWindow onViewDetachedFromWindow:

Callback when the view is attached / detached from the window

registerAdapterDataObserver unregisterAdapterDataObserver:

Observer mode is mainly used to register and unbind adapter data

notifyDataSetChanged notifyItemMoved:

Notify the change of data or item through the adapter and request to update the view

How do we let the adapter handle touch events for us? The method of interface callback can be easily completed. First, we add an internal interface to our adapter, in which the method is implemented when instantiating the view in the first step.

Then call back in onbindviewholder.

3. Connect the recyclerview with the adapter through setadapter, and implement the callback interface inside the adapter.

So far, the simplest recyclerviewadapter has been completed. At the end of the paper, we give a simply encapsulated commonrecycleradapter. When using, we only need to pass in context, layoutresid, list < T > data.

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