1.什么是listview adapter
这个我就不解释了,真有不会的自己在网上查一下(自己不会写开场白,那这个凑活一下,呵呵)
2.直接先看一下效果图吧
和普通的adapter适配基本一样。在开发过程中listview以及gridview是经常使用到的,如果每次出现一个listview或者gridview,那么我们每次都要写一个adapter;这样不仅麻烦而且不便于观察,更重要的是浪费时间。
3.不多说了,看一下这个工具类是怎么使用的:
这个是整个目录结构
adapter中就是工具类,直接复制到项目中使用就可以了
一个实体类,一个activity
4.Activity
public class MainActivity extends AppCompatActivity {
private ListView mList;
private List<TestBean> mStrList = new ArrayList<>();
private CommonAdapter<TestBean> mAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (ListView) findViewById(R.id.m_list);
initData();
initAdapter();
}
private void initData() {
mStrList.add(new TestBean("张三", "http://p2.so.qhmsg.com/t018b83cc648853f307.jpg"));
mStrList.add(new TestBean("李四", "http://img4.duitang.com/uploads/item/201305/02/20130502185029_EkKYh.jpeg"));
mStrList.add(new TestBean("王五","http://img1.touxiang.cn/uploads/20120509/09-014358_953.jpg"));
mStrList.add(new TestBean("赵六", "http://www.qqw21.com/article/UploadPic/2013-9/2013911764015374.jpg"));
mStrList.add(new TestBean("田七", "http://www.qqbody.com/uploads/allimg/201308/07-151626_52.jpg"));
}
private void initAdapter() {
mAdapter = new CommonAdapter<TestBean>(MainActivity.this, mStrList, R.layout.layout_test) {
@Override
public void convert(ViewHolder helper, final TestBean item, int position) {
helper.setText(R.id.m_name, item.getName());
ImageView imageView = helper.getView(R.id.m_image);
Picasso.with(MainActivity.this).load(item.getIcon()).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了"+ item.getName() +"的图像", Toast.LENGTH_SHORT).show();
}
});
}
};
mList.setAdapter(mAdapter);
}
}
main.xml我就不贴了,简直跟平时写的一模一样。主要代码是在initAdapter中。比较简单相信大家能看的懂,conert这个重写方法中和平时写的adapter中的getview是一样的,而且还有简化部分,就不多解释了。
5.工具类 CommonAdapter
public abstract class CommonAdapter<T> extends BaseAdapter {
protected LayoutInflater mInflater;
protected Context mContext;
protected List<T> mDatas;
protected final int mItemLayoutId;
public void cleanList() {
mDatas.clear();
}
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 void jiaNotif(){
notifyDataSetChanged();
}
public abstract void convert(ViewHolder helper, T item, int position);
private ViewHolder getViewHolder(int position, View convertView,ViewGroup parent) {
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,position);
}
}
6.工具类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对象
*/
public static ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
}
return (ViewHolder) convertView.getTag();
}
public View getConvertView() {
return mConvertView;
}
/**
* 通过控件的Id获取对于的控件,如果没有则加入views
*/
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;
}
public ViewHolder setText(int viewId, String text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
public ViewHolder setTextColor(int viewId, int text) {
TextView view = getView(viewId);
view.setTextColor(text);
return this;
}
public ViewHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
public ViewHolder setVisibility(int viewId,int visibility){
View view = getView(viewId);
view.setVisibility(visibility);
return this;
}
public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
public ViewHolder setBackColor(int viewId, int color, int index) {
TextView view = getView(viewId);
view.setBackgroundColor(color);
return this;
}
}
1.一般我们写adapter时,大多传两个参数第一个是Context,主要作用在于找到一个item布局;第二个是list集合,主要作用是设置getcount、getitem等重写方法,但最重要的是在getview方法中填充数据。而其他代码基本是相同的。这样我们就可以将不同的代码提出,相同的代码封装成工具类。
2.CommonAdapter类:
1.通过泛型将list集合中的实体类传过来
2.通过构造方法将集合、layout id传过来
3.通过抽象,对外new当前类实现convert(viewHolder, getItem(position), position)抽象方法
3.ViewHolder类
1.get方法主要是为CommonAdapter的getView提供一个view视图
2.getview方法主要判断views集合里面是否存在当前view,如果存在直接用该view,没有将其存入views
3.set方法主要是对外提供设置,可以根据自己的项目自行添加或者删除方法
7.使用方式(mainactivity里面)
1.private List<实体类> mStrList = new ArrayList<>();
2.private CommonAdapter<实体类> mAdapter = null;
3.mAdapter = new CommonAdapter<实体类>(上下文对象, list集合, 要引用的布局) {
@Override
public void convert(ViewHolder helper, final 实体类 item, int position) {
//该抽象方法是由list集合数量,而循环操作的
//helper可以设置view属性,或者获取一个view
//item是第position个实体类
}
};
mList.setAdapter(mAdapter);