Android开发中总结的Adapter工具类【附完整源码下载】
本文实例讲述了Android开发中总结的Adapter工具类。分享给大家供大家参考,具体如下:
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。
每个开发工程师都会有自己的一些常用到的工具类,今天我分享一下我自己总结的关于Adapter的工具类,话不多说直接上代码
CommonAdapter:
public abstract class CommonAdapter<T> extends BaseAdapter { protected LayoutInflater mInflater; protected Context mContext; protected List<T> mDatas; protected final int mItemLayoutId; public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId) { this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mDatas = mDatas; this.mItemLayoutId = itemLayoutId; } @Override public int getCount() { return mDatas.size(); } @Override public T getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder = getViewHolder(position, convertView, parent); convert(viewHolder, getItem(position) , position); return viewHolder.getConvertView(); } public abstract void convert(ViewHolder helper, T item , int postion); private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) { return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position); } }
ViewHolder:
public class ViewHolder { private final SparseArray<View> mViews; private int mPosition; private View mConvertView; private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.mPosition = position; this.mViews = new SparseArray<View>(); mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false); // setTag mConvertView.setTag(this); } /** * 拿到ViewHolder对象 * * @param context * @param convertView * @param parent * @param layoutId * @param position * @return */ public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(context, parent, layoutId, position); } else { holder = (ViewHolder) convertView.getTag(); holder.mPosition = position; } return holder; } public View getConvertView() { return mConvertView; } /** * 通过控件的Id获取对于的控件,如果没有则加入views * * @param viewId * @return */ public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * 为TextView设置字符串 * * @param viewId * @param text * @return */ public ViewHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView view = getView(viewId); view.setImageBitmap(bm); return this; } /** * 通过图片路径为ImageView设置图片 * @param viewId * @param drawableId * @return */ public ViewHolder setImageByUrl(int viewId, String url) { ImageLoader.getInstance(3,Type.LIFO).loadImage(url, (ImageView) getView(viewId)); return this; } public int getPosition() { return mPosition; } }
MyAdapter:
public class MyAdapter extends CommonAdapter<String> { private List<String> mData ; public MyAdapter(Context context, List<String> mDatas, int itemLayoutId) { super(context, mDatas, itemLayoutId); this.mDatas = mDatas ; } @Override public void convert(final ViewHolder helper, final String item , int postion) { /** * 第一种 */ helper.setImageResource(R.id.item_ib01, R.drawable.ic_launcher); helper.setImageResource(R.id.item_iv01, R.drawable.ic_launcher); helper.setText(R.id.item_tv01, "我是第"+mDatas.get(postion)+"个") ; // 通过路径设置图片 // helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item); /* * 第二种 */ /* * final ImageView item_iv = helper.getView(R.id.item_iv01); final ImageButton item_ib = helper.getView(R.id.item_ib01); TextView item_tv = helper.getView(R.id.item_tv01) ; item_tv.setText("我是第"+mDatas.get(postion)+"个") ; item_iv.setImageResource(R.drawable.ic_launcher); item_ib.setColorFilter(Color.parseColor("#77000000")); */ } }
附:完整实例代码点击此处本站下载。
PS:该下载源码包含了eclipse与studio两个压缩包。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作json格式数据技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
赞 (0)