Android-- ArrayAdapter用法舉例(轉載),androidadapter
近期很多Android開發人員來函表示對ArrayAdapter和BaseAdapter的區別不是很清楚,這裡Android123簡單說下他們的關係和用處,ArrayAdapter是從BaseAdapter派生出來的,具備BaseAdapter的所有功能,但ArrayAdapter更為強大,它執行個體化時可以直接使用泛型構造,我們在Android SDK中可以看到android.widget.ArrayAdapter<T>的字樣,當然也可以使用 ArrayAdapter(Context context, int textViewResourceId) 第二個參數直接綁定一個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檔案來實現自訂顯示效果。
Java代碼
- public class IconListAdapter extends ArrayAdapter<IconListAdapter.IconListItem> {
- protected LayoutInflater mInflater;
- private static final int mResource = R.layout.icon_list_item; //xml布局檔案
-
- public IconListAdapter(Context context,
- List<IconListItem> 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我們可以聯絡到數組的很多操作,沒錯Android123給大家列出本類所有成員方法實用的處理方式,比如:
Java代碼
- void add(T object) //添加一個對象到本ArrayAdapter
-
- void clear() //清除所有元素
-
- static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) //從layout資源構造arrayadapter
-
- Context getContext() //擷取執行個體
-
- int getCount()
-
- View getDropDownView(int position, View convertView, ViewGroup parent) //擷取drop down的popup風格選擇條目的內容,參數1是位置,參數2可以通過強制轉換直接擷取本條的內容
-
- Filter getFilter() //使用正則過濾資料
-
- T getItem(int position) //擷取單條內容
-
- long getItemId(int position)
-
- int getPosition(T item) //通過內容擷取是某條
-
- View getView(int position, View convertView, ViewGroup parent)
-
- void insert(T object, int index) //插入新條目到數組的index位置
-
- void notifyDataSetChanged() //通知數據變化了,告訴綁定Adapter的widget來更新UI
-
- void remove(T object) //移出一條從數組,這裡並沒有指定位置
-
- void setDropDownViewResource(int resource) //設定dropdown的layout風格
- Sets the layout resource to create the drop down views.
-
- void setNotifyOnChange(boolean notifyOnChange) //本條是arrayadapter最強大的功能,android123強烈推薦處理大資料時使用該方法,可以降低ui的處理量,重新整理ui可以更快速,主要可以停止對
- (add(T), insert(T, int), remove(T), clear() 的操作,當然可以通過 notifyDataSetChanged(). 或 setNotifyOnChange(true) 通知變化
-
- void sort(Comparator<? super T> comparator) //這裡是android開發網經常用的排序,使用arrayadapter可以直接排序,十分方便
所以最終android123推薦大家什麼情況使用arrayadapter,什麼時候使用baseadapter。當數量較多,比如超過100條或頻繁動態增減時使用arrayadapter可以方便控制ui,通過setNotifyOnChanage方法,如果比較簡單僅僅呈現直接從 baseadapter更節省資源。