您好,登錄后才能下訂單哦!
這篇文章給大家介紹Android中如何使用Adapter工具類(lèi),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
Adapter是連接后端數(shù)據(jù)和前端顯示的適配器接口,是數(shù)據(jù)和UI(View)之間一個(gè)重要的紐帶。在常見(jiàn)的View(ListView,GridView)等地方都需要用到Adapter。
每個(gè)開(kāi)發(fā)工程師都會(huì)有自己的一些常用到的工具類(lèi),今天我分享一下我自己總結(jié)的關(guān)于Adapter的工具類(lèi),話不多說(shuō)直接上代碼
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對(duì)象 * * @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; } /** * 通過(guò)控件的Id獲取對(duì)于的控件,如果沒(méi)有則加入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; } /** * 為T(mén)extView設(shè)置字符串 * * @param viewId * @param text * @return */ public ViewHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 為ImageView設(shè)置圖片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } /** * 為ImageView設(shè)置圖片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView view = getView(viewId); view.setImageBitmap(bm); return this; } /** * 通過(guò)圖片路徑為ImageView設(shè)置圖片 * @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)+"個(gè)") ; // 通過(guò)路徑設(shè)置圖片 // 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)+"個(gè)") ; item_iv.setImageResource(R.drawable.ic_launcher); item_ib.setColorFilter(Color.parseColor("#77000000")); */ } }
關(guān)于Android中如何使用Adapter工具類(lèi)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。