概述
当前一个项目会有很多的Adapter与它所对应的Model,所以这样会产生很多的Adapter类与Model,这样代码量就会很大,并且还代码重复多,这是一个烦人的过程,所以需要一个万能的模板Adapter,一个Adapter,所有的ListView都能用
ViewHolder模板
对此我直接上源码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78package com.example.againadapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class CommonViewHolder {
private SparseArray< View> mViews;
private View Fview;
/*
* 构造函数,设置Tag,
*/
private CommonViewHolder(Context context, ViewGroup parent, int layoutId,
int position){
this.mViews=new SparseArray<View>();
Fview=LayoutInflater.from(context).inflate(layoutId, parent,false);
Fview.setTag(this);
}
/*
* 得到Tag
*/
public static CommonViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position){
if(convertView==null){
return new CommonViewHolder(context,parent,layoutId,position);
}
return (CommonViewHolder)convertView.getTag();
}
/*
* 最重要获取布局文件中的元素
*/
"unchecked") (
public <T extends View> T getView(int viewId) {
View view = (View) mViews.get(viewId); //从 集合中找,没有找到从当前所在布局文件中找
if (view == null)
{
view = Fview.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getView(){
return Fview;
}
/*
* 以下全是设置元素信息的函数
*/
public void setText(int textId,String textView){
TextView tv=getView(textId);
tv.setText(textView);
}
public void setImageResource(int imageViewId,int imageView){
ImageView im=getView(imageViewId);
im.setImageResource(imageView);
}
public void setImageDrawable(int imageViewId,Drawable drawable){
ImageView im=getView(imageViewId);
im.setImageDrawable(drawable);
}
public void setImageBitmap(int imageViewId,Bitmap bitmap){
ImageView im=getView(imageViewId);
im.setImageBitmap(bitmap);
}
}
以上代码是不是简短,但是很精辟
- 此代码主要在于构造函数,和getView(int viewId)函数,你可以加上Log查看它 的执行过程
Adapter模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62package com.example.againadapter;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class ReplyAdapter<T> extends BaseAdapter{
private List<T> list;
private Context context;
private int layoutId;
public ReplyAdapter(Context context,List<T> list,int layoutId){
this.context=context;
this.list=list;
this.layoutId=layoutId;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int poistion) {
// TODO Auto-generated method stub
return poistion;
}
public long getItemId(int poistion) {
// TODO Auto-generated method stub
return poistion;
}
public View getView(int poistion, View view, ViewGroup viewGroup) {
// TODO Auto-generated method stub
/*ViewHolder viewHolder=null;
view = LayoutInflater.from(context).inflate(
R.layout.replay_text, null);
viewHolder=new ViewHolder();
viewHolder.setText((TextView)view.findViewById(R.id.textView1));
view.setTag(viewHolder);
viewHolder.getText().setText(list.get(poistion));*/
/* CommonViewHolder viewHolder=CommonViewHolder.get(context, view, viewGroup, R.layout.replay_text, poistion);
TextView tv=viewHolder.getView(R.id.textView1);
tv.setText(list.get(poistion));
*/
CommonViewHolder viewHolder=CommonViewHolder.get(context, view, viewGroup, layoutId, poistion);
fun(viewHolder,list.get(poistion));
return viewHolder.getView();
}
abstract void fun(CommonViewHolder viewHolder,T fun);//直接重写此函数,设置你需要设置的元素信息
}
MainActivity
1 | package com.example.againadapter; |
以上是看完张鸿洋博客,依葫芦画瓢写的一个例子,博文地址http://blog.csdn.net/lmj623565791/article/details/38902805/