Simple understanding of the adapter mode in Android

Source: Internet
Author: User

Android

Referring to adapter mode on Android will think of the most common ListView and Baseadapter

In the use of this feature, an object adapter similar to adapter mode

For example in the ListView want to use a GetView () method, but different data, different requirements, there will be different getview () results, so GetView () can not write dead, then may think of the adapter mode

So the ListView contains a ListAdapter member variable, which is actually a ListView that inherits the Abslistview,listadapter variable that is declared in the Abslistview.

Then Baseadapter is implemented ListAdapter, then our custom ***adapter inherited Baseadapter, in fact, it is equivalent to the indirect implementation of listadapter, and * * * Adapter will specifically implement the GetView () method.

Then the ListView can pass the ***adapter in by invoking the Setadapter (ListAdapter adapter) method, thus achieving the results we want.

Java

There are two types of adapter modes:

    1. Class Adapter
    2. Object Adapter

Scene:

If Class A wants to use the M method, the X class has M method, but the result of M method does not necessarily meet the requirement of Class A completely.

So the X class is written dead, not good, so bad design

Then replace the X class with an interface to make some b,c,d,e ... Class Intermediate classes come out, let them all have a way to deal with the M method of things, and then give a class with

Class Adapter:

Design an interface I, let him also have M method

Then design a class B to write a Specialm method that meets the requirements of Class A.

Then let Class A inherit class B and implement the M method of the I Interface

Finally, the Specialm method of Class B is called in Super Mode in M method of Class A.

Object adapters: (More with Object adapters)

Design an interface I, let him also have M method

Then design a class B to write a Specialm method that meets the requirements of Class A.

And then declare a class B variable in a, and class A implements the I interface, then class A also has the M method

Finally, in the M method of Class A, if necessary, you can choose to invoke the Specialm method of Class B.

or design a class B to implement the M method of the I Interface

Then declare an I class variable in Class A, and then call the M method of the I interface directly

Before invoking the M method of Class A, the Class B is set to a member variable of Class A through methods such as Setadapter (I Adapter)

This ensures that the class A and I interfaces are not changed, and when adapting to different situations, write an intermediate class similar to Class B to fit on it.

In short, the two sides are unchanged, through different selection methods, choose a different intermediate class, that is, the adapter mode

Adapter Mode Reference Link

Simple understanding of the adapter mode in Android

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.