ArrayAdapter和BaseAdapter的区别

26 篇文章 0 订阅

近期很多Android开发者来函表示对ArrayAdapterBaseAdapter的区别不是很清楚,这里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文件来实现自定义显示效果。

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给大家列出本类所有成员方法实用的处理方式,比如

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更节省资源

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值