- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractBaseAdapter.java
More file actions
Latest commit
120 lines (97 loc) · 2.77 KB
/
Copy pathAbstractBaseAdapter.java
File metadata and controls
120 lines (97 loc) · 2.77 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
packagemomo.com.week10_project.adapter;
importandroid.content.Context;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importjava.util.List;
/**
* 自定义抽象万能 BaseAdapter
* 可传入任意数据源、布局
*/
publicabstractclassAbstractBaseAdapter<T> extendsBaseAdapter{
//数据源
List<T> data;
//LayoutInflater
LayoutInflaterinflater;
//布局资源
int[] layoutId;
intcountSum = -1;
//构造方法
publicAbstractBaseAdapter(Contextcontext, List<T> data ,int ...layoutId) {
this.layoutId = layoutId;
this.data = data;
inflater = LayoutInflater.from(context);
}
/**
* 定义itemCount
*/
publicvoidsetCount(intcount)
{
countSum = count;
//this.notifyDataSetChanged();
}
@Override
publicintgetCount() {
if (countSum == -1)
{
returndata.size();
}
else
{
returncountSum;
}
}
@Override
publicObjectgetItem(intposition) {
if (countSum == -1)
{
returndata.get(position);
}
else
{
returndata.get(countSum % data.size());
}
}
@Override
publiclonggetItemId(intposition) {
returnposition;
}
//重写getViewTypeCount(因为可能传入的布局有多个)
//返回Listview布局种类个数,即布局资源id数组的长度
@Override
publicintgetViewTypeCount() {
returnlayoutId.length;
}
//抽象绑定数据的方法
publicabstractvoidbindData(intposition, ViewHolderholder);
@Override
publicViewgetView(intposition, ViewconvertView, ViewGroupparent) {
ViewHolderholder = null;
//得到当前数据布局类型
intlayoutType = getItemViewType(position);
if (convertView == null) {
//根据类型加载不同的布局
convertView = inflater.inflate(layoutId[layoutType], parent, false);
holder = newViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//绑定数据
bindData(position, holder);
returnconvertView;
}
publicstaticclassViewHolder {
//保存的控件:是需要设置值的控件
privateViewview;
publicViewHolder(Viewview) {
this.view = view;
}
//向子类提供一个方法,返回需要设置值的控件
publicViewfindViewById(intviewId) {
//根据viewid,找到对应的控件
returnview.findViewById(viewId);
}
}
}