昨天看了一下ArrayAdapter的源代码,发现它还真把BaseAdapter封装的不错。故今天从源码的角度来看看ArrayAdapter。
搞android都知道ArrayAdapter的基本用法,就是在初始化ArrayAdapter的时候,不管你调用那一个构造类,都会调用这个方法:
private void init(Context context, int resource, int textViewResourceId, List<T> objects) { mContext = context; mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mResource = mDropDownResource = resource; mObjects = objects; mFieldId = textViewResourceId; }
然后当你的ListView调用setAdapter的时候,ArrayAdapter就回调用这个方法:
/** * {@inheritDoc} */ public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View view; TextView text; if (convertView == null) { view = mInflater.inflate(resource, parent, false); } else { view = convertView; } try { if (mFieldId == 0) { // If no custom field is assigned, assume the whole resource is a TextView text = (TextView) view; } else { // Otherwise, find the TextView field within the layout text = (TextView) view.findViewById(mFieldId); } } catch (ClassCastException e) { Log.e("ArrayAdapter", "You must supply a resource ID for a TextView"); throw new IllegalStateException( "ArrayAdapter requires the resource ID to be a TextView", e); } T item = getItem(position); if (item instanceof CharSequence) { text.setText((CharSequence)item); } else { text.setText(item.toString()); } return view; }
但我们发现没有,我们ArrayAdapter是一个实体类,而我们在实际工作中大部分用到的的BaseAdapter是一个抽象类,所以我们必须需要重写BaseAdapter中的四个抽象方法,但在ArrayAdapter中我们就不需要了,看代码:
/** * {@inheritDoc} */ public int getCount() { return mObjects.size(); } /** * {@inheritDoc} */ public T getItem(int position) { return mObjects.get(position); } /** * {@inheritDoc} */ public long getItemId(int position) { return position; } /** * {@inheritDoc} */ public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); }
在ArrayAdapter中已经把这四个抽象方法给实现了,那今后我们在设置setAdapter的时候,让我们的自定义Adapter直接继承ArrayAdapter,然后重写getView岂不是更好,然后我们就和那三个基本没有意义的三个抽象方法说拜拜了。
然后我们在看ArrayAdapter,发现它还实现了Filterable这个接口,然后重写了:
/** * {@inheritDoc} */ public Filter getFilter() { if (mFilter == null) { mFilter = new ArrayFilter(); } return mFilter; }
这个方法,有的人会问这个方法有什么用啊,那就得说说Filter这个抽象类了,在ArrayAdapter中有一个成员内部类:
/** * <p>An array filter constrains the content of the array adapter with * a prefix. Each item that does not start with the supplied prefix * is removed from the list.</p> */ private class ArrayFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mOriginalValues == null) { synchronized (mLock) { mOriginalValues = new ArrayList<T>(mObjects); } } if (prefix == null || prefix.length() == 0) { ArrayList<T> list; synchronized (mLock) { list = new ArrayList<T>(mOriginalValues); } results.values = list; results.count = list.size(); } else { String prefixString = prefix.toString().toLowerCase(); ArrayList<T> values; synchronized (mLock) { values = new ArrayList<T>(mOriginalValues); } final int count = values.size(); final ArrayList<T> newValues = new ArrayList<T>(); for (int i = 0; i < count; i++) { final T value = values.get(i); final String valueText = value.toString().toLowerCase(); // First match against the whole, non-splitted value if (valueText.startsWith(prefixString)) { newValues.add(value); } else { final String[] words = valueText.split(" "); final int wordCount = words.length; // Start at index 0, in case valueText starts with space(s) for (int k = 0; k < wordCount; k++) { if (words[k].startsWith(prefixString)) { newValues.add(value); break; } } } } results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { //noinspection unchecked mObjects = (List<T>) results.values; if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } }
这个ArrayFilter内部类实现了这个Filter抽象类的两个抽象方法,一个是performFiltering,一个是publishResult,在performFiltering中实现了过滤条件,那么试想一下,如果公司有搜索联系人这么一个类似需要,布局大约是上边一个EditText,下边是一个ListView,数据都在本地,不用网络获取了,那我们怎么通过输入的文字快速的过滤后展示到ListView中呢,那么这里就有了思路。
我们看一下ArrayFilter中的:
final String valueText = value.toString().toLowerCase();
意思就是把bean中的toString方法作为比较条件,我们是不是在bean中重写一些toString方法,然后在EditText的监听中调用arrayAdapter.getFilter().filter(CharSequence constraint)不就OK了,也有人说了,你自己不会写个过滤条件啊,但是有现成的为什么不用呢。
此外ArrayAdapter里还有一些使用的方法,我们可以慢慢体会,扔掉那个没怎么封装的BaseAdapter吧。