日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

BaseAdapter封装 实现万能适配器

發(fā)布時(shí)間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BaseAdapter封装 实现万能适配器 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.封裝ViewHodler

1 package com.example.utils; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.util.SparseArray; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.ImageView; 10 import android.widget.TextView; 11 12 public class ViewHolder { 13 14 private SparseArray<View> mViews; 15 private int mPostion; 16 private View mConverView; 17 private View view; 18 19 public ViewHolder(Context context, View converview, ViewGroup parent, 20 int layoutid, int postion) { 21 22 this.mPostion = postion; 23 mConverView = LayoutInflater.from(context).inflate(layoutid, parent,false); 24 mViews = new SparseArray<View>(); 25 mConverView.setTag(this); 26 } 27 28 public static ViewHolder get(Context context, View converView, 29 ViewGroup parent, int layoutid, int postion) { 30 31 if (converView == null) { 32 return new ViewHolder(context, converView, parent, layoutid,postion); 33 } else { 34 ViewHolder holder = (ViewHolder) converView.getTag(); 35 holder.mPostion = postion; 36 return holder; 37 } 38 39 } 40 41 public View getConverView() { 42 return mConverView; 43 } 44 45 /** 46 * @author 通過(guò)id獲取控件 47 * 48 * */ 49 50 public <T extends View> T getView(int viewId) { 51 52 View view = mViews.get(viewId); 53 if (view == null) { 54 view = mConverView.findViewById(viewId); 55 mViews.put(viewId, view); 56 } 57 return (T) view; 58 59 } 60 public ViewHolder getTextView(int id,String tv){ 61 62 TextView mTextView = getView(id); 63 mTextView.setText(tv); 64 return this; 65 } 66 67 public ViewHolder getImageResoure(int id,int color){ 68 69 ImageView img=getView(id); 70 img.setImageResource(color); 71 return this; 72 } 73 74 public ViewHolder getImageBitmap(int id,Bitmap map){ 75 76 ImageView view=getView(id); 77 view.setImageBitmap(map); 78 return this; 79 } 80 }

2. ? ?封裝BaseAdapter

? ??

1 package com.example.utils; 2 3 import java.util.List; 4 import android.content.Context; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 10 public abstract class CommonAdapter<T> extends BaseAdapter { 11 12 protected Context mContext; 13 protected List<T> mDatas; 14 protected LayoutInflater mInflater; 15 protected int mlayoutId; 16 public CommonAdapter(Context context,List<T> data,int layoutId) { 17 18 this.mContext=context; 19 this.mDatas=data; 20 mInflater=LayoutInflater.from(mContext); 21 this.mlayoutId=layoutId; 22 } 23 24 @Override 25 public int getCount() { 26 return mDatas.size(); 27 } 28 29 @Override 30 public T getItem(int postion) { 31 return mDatas.get(postion); 32 } 33 34 @Override 35 public long getItemId(int postion) { 36 return postion; 37 } 38 39 @Override 40 public View getView(int postion, View converview, ViewGroup group){ 41 ViewHolder holder=ViewHolder.get(mContext, converview, group, mlayoutId, postion); 42 convert(holder, getItem(postion)); 43 return holder.getConverView(); 44 }; 45 46 public abstract void convert(ViewHolder holder,T t); 47 }

3.自己的MyAdapter

??

1 package com.example.utils; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import com.example.adapter.R; 7 import com.example.bean.Bean; 8 9 public class MyAdapter extends CommonAdapter<Bean>{ 10 11 public MyAdapter(Context context, List<Bean> data, int layoutId) { 12 super(context, data, layoutId); 13 } 14 15 @Override 16 public void convert(ViewHolder holder, Bean bean) { 17 18 holder.getTextView(R.id.tv_title, bean.getTitle()); 19 holder.getTextView(R.id.tv_time, bean.getTime()); 20 holder.getTextView(R.id.tv_from, bean.getForm()); 21 } 22 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/lgy0069/p/5419332.html

總結(jié)

以上是生活随笔為你收集整理的BaseAdapter封装 实现万能适配器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。