-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
CheckAdapter.java
204 lines (185 loc) · 4.95 KB
/
CheckAdapter.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.lazylibrary.util;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* <h2>选择适配器</h2>
* <br>准备工作:
* <br>第一步:创建你的Item并实现CheckAdapter.CheckItem接口
* <br>第二步:创建你的适配器并继承于CheckAdapter并同时通过泛型指定Item类型
* <br>第三步:在构造函数中传给父类Item集合
* <br>第四步:在getView()方法中调用handleCheckBox()方法处理CheckBox
* <br>第五步:在ListView的OnItemClickListener中调用clickIitem()方法处理点击事件
* <br>至此,全部准备工作已经完结
* <br>
* <br>接下来你可以:
* <br>调用setSingleMode()方法修改选择模式为单选模式(默认是多选模式)
* <br>调用enableCheckMode()方法开启选择模式
* <br>调用cancelCheckMode()方法取消选择模式
* <br>调用isEnableCheckMode()方法判断是否正处于选择模式
* <br>调用getCheckedItems()方法获取全部选中的项
* <br>调用deleteCheckedItems()方法删除全部选中的项
*/
public abstract class CheckAdapter<T extends CheckAdapter.CheckItem> extends
BaseAdapter {
private List<T> items; //列表项
private boolean enabledCheckMode; //激活选择模式
private boolean singleMode; //单选模式
private int currentCheckedPosition = -1;
public CheckAdapter(List<T> items) {
this.items = items;
}
@Override
public int getCount() {
return items != null ? items.size() : 0;
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
notifyDataSetChanged();
}
/**
* 是否是单选模式
* @return
*/
public boolean isSingleMode() {
return singleMode;
}
/**
* 设置单选模式,默认是复选模式
* @param singleMode
*/
public void setSingleMode(boolean singleMode) {
this.singleMode = singleMode;
}
/**
* 处理复选框
* @param compoundButton
* @param t
*/
public void handleCompoundButton(CompoundButton compoundButton, T t){
compoundButton.setChecked(t.isChecked());
compoundButton.setVisibility(isEnabledCheckMode() ? View.VISIBLE : View.GONE);
}
/**
* 激活选择模式
*/
public void enableCheckMode(){
if(enabledCheckMode){
return;
}
enabledCheckMode = true;
notifyDataSetChanged();
}
/**
* 取消选择模式
*/
public void cancelCheckMode(){
if(!enabledCheckMode){
return;
}
enabledCheckMode = false;
for(T item : items){
item.setChecked(false);
}
notifyDataSetChanged();
}
/**
* 判定是否已激活选择模式
* @return
*/
public boolean isEnabledCheckMode() {
return enabledCheckMode;
}
/**
* 点击了某一项
* @param postion
* @return true:已经激活了选择模式并且设置成功;false:尚未激活选择模式并且设置失败
*/
public boolean clickItem(int postion){
if(isEnabledCheckMode()){
if(postion < items.size()){
if(singleMode){
if(currentCheckedPosition == -1 || currentCheckedPosition == postion){
T item = items.get(postion);
item.setChecked(!item.isChecked());
}else{
items.get(currentCheckedPosition).setChecked(false);
items.get(postion).setChecked(true);
}
currentCheckedPosition = postion;
}else{
T item = items.get(postion);
item.setChecked(!item.isChecked());
}
notifyDataSetChanged();
}
return true;
}else{
return false;
}
}
/**
* 获取选中的项
* @return
*/
public List<T> getCheckedItems(){
List<T> checkedItems = new ArrayList<T>();
for(T item : items){
if(item.isChecked()){
checkedItems.add(item);
}
}
return checkedItems;
}
/**
* 删除选中的项
* @return
*/
public List<T> deleteCheckedItems(){
List<T> checkedItems = new ArrayList<T>();
Iterator<T> iterator = items.iterator();
T item;
while(iterator.hasNext()){
item = iterator.next();
if(item.isChecked()){
checkedItems.add(item);
iterator.remove();
}
}
notifyDataSetChanged();
return checkedItems;
}
public interface CheckItem {
public boolean isChecked();
public void setChecked(boolean checked);
}
}