您的位置:首页 > 移动开发 > Android开发

个人总结android中的BaseAdapter

2016-03-03 09:43 363 查看
本章不讨论Android中的BaseAdapter的工作原理,只是将其封装成一个通用的Adapter。在Android5.0有了RecyclerView的封装好的RecyclerView.Adapter,但是如果你的版本过低可能会不兼容,所以就封装了一下,希望能给阅读者帮助,如果有不当之处,望能指点更正。

public abstract class ParentAdapter<T> extends BaseAdapter {

/**

* 集合

*/

protected List<T> mLists = null;

/**

* 数组

*/

protected T[] mArays;

/**

* 上下文

*/

protected Context mContext;

/**

* 填充布局文件的类

*/

protected LayoutInflater mInflater;

/**

* 只适用于填充数据集合 : List<T>

*

* @param ctx

*/

public ParentAdapter(Context ctx) {

mLists = new ArrayList<T>();

this.mContext = ctx;

this.mInflater = LayoutInflater.from(ctx);

}

/**

* 构造函数

*

* @param ctx

* 上下文

* @param lists

* 集合

*/

public ParentAdapter(Context ctx, List<T> lists) {

this.mLists = lists;

if (null == lists) {

mLists = new ArrayList<T>();

}

this.mContext = ctx;

this.mInflater = LayoutInflater.from(ctx);

}

/**

* 构造函数

*

* @param ctx

* 上下文

* @param mArays

* 数组

* @throws Exception

* 检查数组是否为空

*/

public ParentAdapter(Context ctx, T[] mArays) throws Exception {

if (null == mArays) {

throw new Exception("mArays not null !!!");

}

this.mArays = mArays;

this.mContext = ctx;

this.mInflater = LayoutInflater.from(ctx);

}

@Override

public int getCount() {

if (null != mLists) {

return mLists.size();

} else if (null != mArays) {

return mArays.length;

}

return 0;

}

@Override

public T getItem(int position) {

if (null != mLists) {

return mLists.get(position);

} else if (null != mArays) {

return mArays[position];

}

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public abstract View getView(int position, View convertView, ViewGroup parent);

/**

* 设置数据,以前数据会清空

*

* @param collections

*/

public boolean setDatas(Collection<T> collections) {

try {

if (null == mLists) {

return false;

}

mLists.clear();

if (null != collections && 0 != collections.size()) {

mLists.addAll(collections);

}

notifyDataSetChanged();

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 在原始数据上添加新数据 添加单项

*

* @param t

*/

public boolean addData(T t) {

try {

if (null == mLists) {

return false;

}

if (null != t) {

mLists.add(t);

notifyDataSetChanged();

}

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 在原始数据上添加新数据 <功能详细描述> 添加集合

*

* @param collections

*/

public boolean addData(Collection<T> collections) {

try {

if (null == mLists) {

return false;

}

if (null != collections && 0 != collections.size()) {

mLists.addAll(collections);

notifyDataSetChanged();

}

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 清除所有集合元素

*/

public boolean clearAllData() {

try {

if (null == mLists) {

return false;

}

if (0 == mLists.size()) {

return true;

}

mLists.clear();

notifyDataSetChanged();

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 获取mLists或者mArrays的总的行数

*

* @return

*/

public int getTotalSize() {

if (null != mLists) {

return mLists.size();

} else if (null != mArays) {

return mArays.length;

}

return 0;

}

/**

* 获取mLists

*

* @return

*/

public List<T> getMLists() {

return mLists;

}

/**

* 获取mArays

*

* @return

*/

public T[] getMArays() {

return mArays;

}

/**

* 移除单独的一个元素

*

* @param position

*/

public boolean removeData(int position) {

try {

if (null == mLists) {

return false;

}

if (position < getTotalSize()) {

mLists.remove(position);

notifyDataSetChanged();

return true;

}

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 获取mLists或者mArrays其中位置的数据

*

* @param index

* 所在的集合或者数组中的位置,不是在Adapter中的位置

* @return

*/

public T getData(int index) {

if (index < getTotalSize()) {

try {

if (null != mLists) {

return mLists.get(index);

} else if (null != mArays) {

return mArays[index];

}

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

/**

* 更新局部信息

*

* @param position

* @param t

* @return

*/

public boolean updateData(int position, T t) {

try {

if (position >= 0 && position < getTotalSize()) {

mLists.set(position, t);

notifyDataSetChanged();

}

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: