The relationship between adapter and Adapterview

Source: Internet
Author: User

General statement

Android "List" Implementation is actually a typical MVC pattern, in fact, Adapterview is equivalent to view, responsible for drawing and view of the event response, adapter equivalent to controller, is responsible for controlling the display of data and presentation methods, In addition, the entity class in the project represents model.

Adapter

Adapter is actually an interface, not a specific class. Its main use is as a bridge between the Adapterview and model, which is clearly defined in the source code:

The code is as follows Copy Code
/**
* An Adapter object acts as a bridge between a {@link Adapterview} and the
* Underlying data for that view. The Adapter provides access to the data items.
* The Adapter is also responsible to making a {@link Android.view.View} for
* ... ...
*/

GetView () is one of the most important functions of adapter, and the primary function of this function is to show different data based on the position in the list. Details can be detailed in this method of source code comments, the above has been written very clearly.
The other is Registerdatasetobserver () and Unregisterdatasetobserver (), well, it should be a pair, which is actually a typical observer design pattern, If the data that needs to be loaded in the adapter changes, we are notifying adapter to update the data. Of course, we typically use the notifydatasetchanged () method in our projects because Baseadapter not only inherits adapter, but it also encapsulates some of these methods. This includes the Datasetobservable notifychanged () method, the source code is as follows:

The code is as follows Copy Code

/**
* Notifies the attached observers that underlying data has been changed
* and any View reflecting the data set should refresh itself.
*/
public void notifydatasetchanged () {
Mdatasetobservable.notifychanged ();
}
Adapterview

Adapterview is also an abstract class, such as Ablistview, which is inherited from it. Adapterview is mainly the setting of some listeners, such as:

Item Length by listener

The code is as follows Copy Code

Public interface Onitemlongclicklistener {
/**
* Callback is invoked when a item in this view has been
* Clicked and held.
*... ...
*/
Boolean Onitemlongclick (adapterview<?> parent, view view, int position, long ID);
}

Item Click Listener

The code is as follows Copy Code

Public interface Onitemclicklistener {

/**
* Callback is invoked when a item in this adapterview has
* Been clicked.
*... ...
*/
void Onitemclick (adapterview<?> parent, view view, int position, long ID);
}

There are also the most common and basic methods, such as:

The code is as follows Copy Code

/**
* Returns the adapter currently associated with this widget.
*... ...
*/
Public abstract T Getadapter ();
/**
* Sets The adapter that provides the data
* in this widget.
*... ...
*/
public abstract void Setadapter (T adapter);

Although Adapterview is only an abstract class, but the "dry goods" is indeed not less, there are many useful but not commonly used methods, such as:

The code is as follows Copy Code

public Boolean Performitemclick (view view, int position, long ID) {
if (Monitemclicklistener!= null) {
Playsoundeffect (Soundeffectconstants.click);
if (view!= null) {
View.sendaccessibilityevent (accessibilityevent.type_view_clicked);
}
Monitemclicklistener.onitemclick (this, view, position, id);
return true;
}
return false;
}

As the name suggests this is a way to achieve automatic click Item, when you need to use directly can save a lot of effort.

Conclusion

In fact, because the last article to explore the Android ListView display disorder problem, caused me to explore the listview of the internal source of interest, of course, because the level of limited only from the perspective of the relatively shallow to explore, there will be time to continue to read the source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.