标签:
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
package
com.phodev.andtools.widget; import
android.content.Context; import
android.content.res.Resources.NotFoundException; import
android.database.DataSetObserver; import
android.graphics.drawable.Drawable; import
android.util.AttributeSet; import
android.view.View; import
android.view.View.OnClickListener; import
android.widget.BaseAdapter; import
android.widget.ImageView; import
android.widget.LinearLayout; import
android.widget.ListAdapter; /** *
LinearLayout模仿ListView,解决ListView在ScrollView中的不兼容问题 *
*
@author skg *
*/ public
class
RigidListView extends
LinearLayout implements
OnClickListener { private
BaseAdapter mAdapter; private
OnItemClickListener onItemClickListener; private
Drawable dividerDrawable; private
int
dividerResId = android.R.drawable.divider_horizontal_dim_dark; private
int
itemSelectorDrawableResId = android.R.drawable.list_selector_background; /** *
绑定布局 */ private
void
bindLinearLayout() { removeAllChild(); int
count = mAdapter.getCount(); for
( int
i = 0 ;
i < count; i++) { View
v = mAdapter.getView(i, null ,
null ); v.setLayoutParams( new
LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // LinearLayout
itemContainer = new
LinearLayout(getContext()); itemContainer.setLayoutParams( new
LayoutParams( LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)); //
添加Item的View itemContainer.addView(v); //
设置Divider itemContainer.setBackgroundResource(itemSelectorDrawableResId); itemContainer.setOrientation(LinearLayout.VERTICAL); itemContainer.setId(i); itemContainer.setClickable( true ); if
(onItemClickListener != null )
{ itemContainer.setOnClickListener( this ); } itemContainer.setFocusable( true ); Drawable
divider = getDivider(); if
(divider != null )
{ ImageView
line = new
ImageView(getContext()); line.setBackgroundDrawable(dividerDrawable); itemContainer.addView(line); } addView(itemContainer,
i); } } public
RigidListView(Context context) { super (context); init(context); } public
RigidListView(Context context, AttributeSet attrs) { super (context,
attrs); init(context); } private
void
init(Context context) { this .setOrientation(LinearLayout.VERTICAL); } /** *
不设置的话使用默认的,0表示不使用任何Divider *
*
@param resId */ public
void
setDivider( int
resId) { dividerResId
= resId; } public
void
setItemSelector( int
resId) { itemSelectorDrawableResId
= resId; } private
Drawable getDivider() { if
(dividerDrawable == null
&& dividerResId != 0 )
{ try
{ dividerDrawable
= getContext().getResources().getDrawable( dividerResId); }
catch
(NotFoundException e) { e.printStackTrace(); } } return
dividerDrawable; } /** *
获取Adapter *
*
@return adapter */ public
ListAdapter getAdpater() { return
mAdapter; } /** *
设置数据 *
*
@param adpater */ public
void
setAdapter(BaseAdapter adpater) { mAdapter
= adpater; mAdapter.registerDataSetObserver(dataSetObserver); bindLinearLayout(); } /** *
设置点击事件,OnItemClickListener不同于ListView的监听,这个监听必须在setAdapter之前才有效 *
*
@param onItemClickListener */ public
void
setOnItemClickListener(OnItemClickListener onItemClickListener) { this .onItemClickListener
= onItemClickListener; } public
interface
OnItemClickListener { public
void
onItemClick(View view, ListAdapter adpater, int
position); } @Override public
void
onClick(View v) { if
(onItemClickListener != null )
{ onItemClickListener.onItemClick(v,
mAdapter, v.getId()); } } private
DataSetObserver dataSetObserver = new
RigidDataSetObserver(); public
class
RigidDataSetObserver extends
DataSetObserver { @Override public
void
onChanged() { refreshUI(); } @Override public
void
onInvalidated() { refreshUI(); } } /** *
刷新UI */ private
void
refreshUI() { bindLinearLayout(); } private
void
removeAllChild() { if
( this .getChildCount()
> 0 )
{ removeAllViews(); } } } |
标签:
原文地址:http://blog.csdn.net/u014311037/article/details/42644633