色yeye在线视频观看_亚洲人亚洲精品成人网站_一级毛片免费播放_91精品一区二区中文字幕_一区二区三区日本视频_成人性生交大免费看

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 淺談android中的適配器模式

淺談android中的適配器模式 時間:2018-09-26      來源:未知

引言:在長期的android教學中,不難發現ListView是一個讓學生十分頭痛的組件,原因很簡單,這里有個適配器的概念,對于學生來講是一個從未涉及的新的領域,那么我們就通過這邊文章來初識一下軟件設計模式中的適配器模式。

1.適配器模式的定義:

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatile interfaces.

將一個類的接口變成客戶端所期待的另一中接口,從而使原本因接口不匹配而無法在一起工作的兩個類能夠在一起工作。

其中用到適配器模式的經典例子就是插座匹配問題,直接給圖:

2.適配器模式的分類:

適配器模式主要分為兩種:類適配器和對象適配器

如上圖所示(截取自《Head First Design Patterns》一書),主要包括三個部分:

1) Target目標角色。該角色定義把其他類轉換為我們的期待接口。

2) Adaptee源角色。就是原始的類和接口對象,它是已經存在的,只是不符合現有的要求,而需要利用適配器角色的包裝。

3) Adapter適配器角色。適配器模式的核心角色,其它兩個角色都是已經存在的角色,而適配器角色是需要新建立的,它的職責非常簡單;把源角色轉換為目標角色;通過繼承或是組合的方式。

3.適配器模式的優勢:

1)適配器模式可以讓兩個沒有任何關系的類在一起運行,只要適配器這個角色能夠搞定它們。

2)增加了類的通透性,我們訪問的Target目標角色,但是具體的實現都委托給了源角色,而這些對高層次模塊是透明的,也是它不需要關心得。

3)調高了類的復用性和靈活性非常好。如果覺得適配器不夠好,只要修改適配器就行,其它的代碼都不用修改,適配器就是一個靈活的構件,想用就用。

4.適配器模式在android源碼中的應用:

在Android源碼中,ListView中用到的就是適配器模式。ListView用于顯示列表數據,但列表數據形式多種多樣(),為了處理和顯示不同的數據,我們需要對應的適配器作為橋梁。

在ListView中有一個變量ListAdapter mAdapter;是顯示在view試圖上的數據:

/**

* The adapter containing the data to be displayed by this view

*/

ListAdapter mAdapter;

在ListAdapter中定義了所需要的接口函數:

package android.widget;

/**

* Extended {@link Adapter} that is the bridge between a {@link ListView}

* and the data that backs the list. Frequently that data comes from a Cursor,

* but that is not

* required. The ListView can display any data provided that it is wrapped in a

* ListAdapter.

*/public interface ListAdapter extends Adapter {

/**

* Indicates whether all the items in this adapter are enabled. If the

* value returned by this method changes over time, there is no guarantee

* it will take effect. If true, it means all items are selectable and

* clickable (there is no separator.)

*

* @return True if all items are enabled, false otherwise.

*

* @see #isEnabled(int)

*/

public boolean areAllItemsEnabled();

/**

* Returns true if the item at the specified position is not a separator.

* (A separator is a non-selectable, non-clickable item).

*

* The result is unspecified if position is invalid. An {@link ArrayIndexOutOfBoundsException}

* should be thrown in that case for fast failure.

*

* @param position Index of the item

*

* @return True if the item is not a separator

*

* @see #areAllItemsEnabled()

*/

boolean isEnabled(int position);

}

它是繼承自Adapter:

其中Adapter定義了getCount()、getItemViewType(int position)等接口函數。

此時的ListAdapter就是一個Target目標角色,而我們的ListView就是一個Client。因此為了適配和顯示一些數據,如Cursor等,所以就需要相應的適配器CursorAdapter,代碼如下:

public abstract class CursorAdapter extends BaseAdapter implements Filterable,

CursorFilter.CursorFilterClient {。。。

protected Cursor mCursor;

protected ChangeObserver mChangeObserver;

protected DataSetObserver mDataSetObserver;

protected CursorFilter mCursorFilter;

。。。

/**

* Returns the cursor.

* @return the cursor.

*/

public Cursor getCursor() {

return mCursor;

}

// 實現ListAdapter目標接口的getCount函數,通過返回源角色mCursor的方法getCount函數

/**

* @see android.widget.ListAdapter#getCount()

*/

public int getCount() {

if (mDataValid && mCursor != null) {

return mCursor.getCount();

} else {

return 0;

}

}

// 實現ListAdapter目標接口的getItem函數,通過返回源角色mCursor的方法moveToPosition函數

/**

* @see android.widget.ListAdapter#getItem(int)

*/

public Object getItem(int position) {

if (mDataValid && mCursor != null) {

mCursor.moveToPosition(position);

return mCursor;

} else {

return null;

}

}

// 實現ListAdapter目標接口的getItemId函數,通過返回源角色mCursor的方法getLong函數

/**

* @see android.widget.ListAdapter#getItemId(int)

*/

public long getItemId(int position) {

if (mDataValid && mCursor != null) {

if (mCursor.moveToPosition(position)) {

return mCursor.getLong(mRowIDColumn);

} else {

return 0;

}

} else {

return 0;

}

}

@Override

public boolean hasStableIds() {

return true;

}

其中源角色Cursor接口如下所示:

public interface Cursor {

。。。

/**

* Returns the numbers of rows in the cursor.

*

* @return the number of rows in the cursor.

*/

int getCount();

/**

* Returns the current position of the cursor in the row set.

* The value is zero-based. When the row set is first returned the cursor

* will be at positon -1, which is before the first row. After the

* last row is returned another call to next() will leave the cursor past

* the last entry, at a position of count().

*

* @return the current cursor position.

*/

int getPosition();

。。。

/**

* Move the cursor to an absolute position. The valid

* range of values is -1 <= position <= count.

*

*

This method will return true if the request destination was reachable,

* otherwise, it returns false.

*

* @param position the zero-based position to move to.

* @return whether the requested move fully succeeded.

*/

boolean moveToPosition(int position);

。。。

/**

* Returns the value of the requested column as a long.

*

*

The result and whether this method throws an exception when the

* column value is null, the column type is not an integral type, or the

* integer value is outside the range [Long.MIN_VALUE,

Long.MAX_VALUE] is implementation-defined.

*

* @param columnIndex the zero-based index of the target column.

* @return the value of that column as a long.

*/

long getLong(int columnIndex);

。。。

}

這就將Cursor類型接口通過CursorAdapter適配器轉換成目標角色ListAdapter目標接口,繼而讓ListView使用,并展示。

以上就是android中關于ListView的適配器模式使用一些簡單分享,希望對大家學習ListView有所幫助。

上一篇:Unik3-復原過程

下一篇:如何實現Arm處理器ICache的測試

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 1234成人网站 | 伊人色综合久久天天五月婷 | 亚洲va天堂va在线va欧美 | 永久免费a片在线观看全网站 | 色五月丁香六月欧美综合 | 怡红院成永久免费人视频视色 | 无码一卡二卡三卡四卡 | 亚洲av永久无码精品古装片 | 做爰爽全过程免费的看 | free性欧美人与牛 | 久久强奷乱码老熟女网站 | 又黄又大又爽A片三年片 | 福利在线观看1000集 | 欧美大色网 | 成年人在线免费看的惊悚动作片 | 国精产品999国精产品官网 | 99久久婷婷 | 夜夜高潮夜夜爽精品欧美做爰 | 国产成人无码3000部 | 欧美疯狂性受XXXXX另类 | 99好久被狂躁A片视频无码刻晴 | 国产美女裸体丝袜喷水视频 | 亚洲最大日夜无码中文字幕 | 亚洲浮力影院久久久久久 | 久久国产加勒比精品无码 | 国产乱人偷精品人妻a片 | 日本乱人伦片中文三区 | 欧洲一卡2卡3卡4卡国产 | 精品无码免费专区毛片 | 高H紫黑色的又粗又上翘 | 大地资源视频在线观看 | 色哟哟网站在线观看 | 啊啊啊啊好爽在线观看 | 国产自在线拍 | 开心综合激激的五月天的 | 国产极品美女高潮无套在线观看 | av在线观看网址 | 亚洲色爱免费观看视频 | 色哟哟在线观看免费高清大 | 日本hd高清xxxxvideos | 真实国产普通话对白乱子子伦视频 |