现在的位置: 主页 > 商讯 > 文章列表

Android超级common的Adapter,也就是传说的CommonAdap

作者:北京东方雨泽科技有限公司 来源:www.dfyz6789.com 发布时间:2017-09-05 12:28:10
 

Android超级common的Adapter,也就是传说的CommonAdapter

有了这个CommonAdapter以后的Adapter就so easy 了。

1. 定义CommonAdapter

package com.stephen.ifishing.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; /** * * @author Stephen Huang * * @param */ public abstract class CommonAdapter extends BaseAdapter { protected Context mContext; protected List mDatas; protected LayoutInflater mInflater; protected int layoutId; public CommonAdapter(Context context, List data, int layoutId) { this.mContext = context; mInflater = LayoutInflater.from(context); this.mDatas = data; this.layoutId = layoutId; } @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; } public void onDataChange(List data) { this.mDatas = data; this.notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position); convert(holder, mDatas.get(position)); return holder.getConvertView(); } public abstract void convert(ViewHolder holder, T t); } 2. 辅助类ViewHolder package com.stephen.ifishing.adapter; import android.content.Context; import android.graphics.Bitmap; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; /** * * @author Stephen Huang * */ public class ViewHolder { private SparseArray mViews; private int mPostion; private View mConvertView; public ViewHolder(Context context, ViewGroup parent, int layoutId, int postion) { this.mPostion = postion; this.mViews = new SparseArray(); this.mConvertView = LayoutInflater.from(context).inflate(layoutId, null); mConvertView.setTag(this); } public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int postion) { if (convertView == null) { return new ViewHolder(context, parent, layoutId, postion); } else { ViewHolder holder = (ViewHolder)convertView.getTag(); holder.mPostion = postion; return holder; } } /** * 通过viewId获取控件 * @param viewId * @return */ public T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public View getConvertView() { return mConvertView; } public ViewHolder setText(int viewId, String text) { TextView tv = getView(viewId); tv.setText(text); return this; } public ViewHolder setImageResource(int viewId, int resId) { ImageView iv = getView(viewId); iv.setImageResource(resId); return this; } public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView iv = getView(viewId); iv.setImageBitmap(bm); return this; } }
3. 如何使用我们的CommonAdapter? package com.stephen.ifishing.adapter; import java.util.List; import com.stephen.ifishing.R; import com.stephen.ifishing.model.ShowFishingItem; import android.content.Context; /** * * @author Stephen Huang * */ public class ShowFishingAdapter extends CommonAdapter { public ShowFishingAdapter(Context context, List itemList) { super(context, itemList, R.layout.show_fishing); } @Override public void convert(ViewHolder holder, ShowFishingItem bean) { holder.setText(R.id.tv_user_name, bean.userName) .setText(R.id.tv_say_something, bean.saySomething) .setImageResource(R.id.iv_portrait, bean.portraitImageResId); } }
当然没有最好,只有更好,还有很多可以改进的地方哟!

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站推广 http://www.feimao666.com

  • 上一篇:ASP.NET MVC IOC 之AutoFac攻略
  • 下一篇:最后一页
  •