近期很多Android開發(fā)者來函表示對ArrayAdapter和BaseAdapter的區(qū)別不是很清楚,這里Android123簡單說下他們的關系和用處,ArrayAdapter是從BaseAdapter派生出來的,具備BaseAdapter的所有功能,但ArrayAdapter更為強大,它實例化時可以直接使用泛型構造,我們在Android SDK中可以看到android.widget.ArrayAdapter的字樣,當然也可以使用ArrayAdapter(Context context, int textViewResourceId) 第二個參數(shù)直接綁定一個layout,下文的例子我們使用Java泛型實例化。
通過Adapter我們構造一個支持icon的item,下面我們在getView中使用的是imageView顯示圖片,當然android123提示大家其實TextView也可以直接綁定一個drawable對象顯示的,void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 或void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) 和void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) 即可,其中第二種的int類型指定的資源id,方位則是textview什么位置顯示drawable對象
說了這么多ArrayAdapater一起看個例子,來實例化ArrayAdapter吧,我們可以修改Res/layout/icon_list_item.xml文件來實現(xiàn)自定義顯示效果。
public class IconListAdapter extends ArrayAdapter {
protected LayoutInflater mInflater;
private static final int mResource = R.layout.icon_list_item; //xml布局文件
public IconListAdapter(Context context,
List items) {
super(context, mResource, items);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
ImageView image;
View view;
if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
} else {
view = convertView;
}
text = (TextView) view.findViewById(R.id.text1);
text.setText(getItem(position).getTitle());
image = (ImageView) view.findViewById(R.id.icon); //可以使用上文說的三種方法,直接用TextView類的setCompoundDrawables等方法綁定圖標顯示
image.setImageResource(getItem(position).getResource());
return view;
}
public static class IconListItem { //每條顯示的構造方法
private final String mTitle;
private final int mResource;
public IconListItem(String title, int resource) {
mResource = resource;
mTitle = title;
}
public String getTitle() {
return mTitle;
}
public int getResource() {
return mResource;
}
}
}
當然對于ArrayAdapter到底比BaseAdapter先進到哪里呢? 從名稱來看Array我們可以聯(lián)系到數(shù)組的很多操作,沒錯Android123給大家列出本類所有成員方法實用的處理方式,比如
void add(T object) //添加一個對象到本ArrayAdapter
void clear() //清除所有元素
static ArrayAdapter createFromResource(Context context, int textArrayResId, int textViewResId) //從layout資源構造arrayadapter
Context getContext() //獲取實例
int getCount()
View getDropDownView(int position, View convertView, ViewGroup parent) //獲取drop down的popup風格選擇條目的內(nèi)容,參數(shù)1是位置,參數(shù)2可以通過強制轉換直接獲取本條的內(nèi)容
Filter getFilter() //使用正則過濾數(shù)據(jù)
T getItem(int position) //獲取單條內(nèi)容
long getItemId(int position)
int getPosition(T item) //通過內(nèi)容獲取是某條
View getView(int position, View convertView, ViewGroup parent)
void insert(T object, int index) //插入新條目到數(shù)組的index位置
void notifyDataSetChanged() //通知數(shù)據(jù)變化了,告訴綁定Adapter的widget來更新UI
void remove(T object) //移出一條從數(shù)組,這里并沒有指定位置
void setDropDownViewResource(int resource) //設置dropdown的layout風格
Sets the layout resource to create the drop down views.
void setNotifyOnChange(boolean notifyOnChange) //本條是arrayadapter最強大的功能,android123強烈推薦處理大數(shù)據(jù)時使用該方法,可以降低ui的處理量,刷新ui可以更快速,主要可以停止對
(add(T), insert(T, int), remove(T), clear() 的操作,當然可以通過 notifyDataSetChanged(). 或 setNotifyOnChange(true) 通知變化
void sort(Comparator comparator) //這里是android開發(fā)網(wǎng)經(jīng)常用的排序,使用arrayadapter可以直接排序,十分方便
所以最終android123推薦大家什么情況使用arrayadapter,什么時候使用baseadapter。當數(shù)量較多,比如超過100條或頻繁動態(tài)增減時使用arrayadapter可以方便控制ui,通過setNotifyOnChanage方法,如果比較簡單僅僅呈現(xiàn)直接從baseadapter更節(jié)省資源
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請
點擊舉報。