-
Notifications
You must be signed in to change notification settings - Fork 219
/
ContactAdapter.java
235 lines (198 loc) · 8.52 KB
/
ContactAdapter.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* created by jiang, 12/3/15
* Copyright (c) 2015, jyuesong@gmail.com All Rights Reserved.
* * # #
* # _oo0oo_ #
* # o8888888o #
* # 88" . "88 #
* # (| -_- |) #
* # 0\ = /0 #
* # ___/`---'\___ #
* # .' \\| |# '. #
* # / \\||| : |||# \ #
* # / _||||| -:- |||||- \ #
* # | | \\\ - #/ | | #
* # | \_| ''\---/'' |_/ | #
* # \ .-\__ '-' ___/-. / #
* # ___'. .' /--.--\ `. .'___ #
* # ."" '< `.___\_<|>_/___.' >' "". #
* # | | : `- \`.;`\ _ /`;.`/ - ` : | | #
* # \ \ `_. \_ __\ /__ _/ .-` / / #
* # =====`-.____`.___ \_____/___.-`___.-'===== #
* # `=---=' #
* # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
* # #
* # 佛祖保佑 永无BUG #
* # #
*/
package com.jiang.android.indexrecyclerview.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jiang.android.indexrecyclerview.MainActivity;
import com.jiang.android.indexrecyclerview.R;
import com.jiang.android.indexrecyclerview.model.ContactModel;
import com.jiang.android.indexrecyclerview.widget.IndexAdapter;
import com.jiang.android.lib.adapter.BaseAdapter;
import com.jiang.android.lib.adapter.expand.StickyRecyclerHeadersAdapter;
import com.jiang.android.lib.widget.SwipeItemLayout;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jiang on 12/3/15.
* 根据当前权限进行判断相关的滑动逻辑
*/
public class ContactAdapter extends BaseAdapter<ContactModel.MembersEntity,ContactAdapter.ContactViewHolder>
implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder>,IndexAdapter
{
/**
* 当前处于打开状态的item
*/
private List<SwipeItemLayout> mOpenedSil = new ArrayList<>();
private List<ContactModel.MembersEntity> mLists;
private Context mContext;
private int mPermission;
private String createrID;
private boolean isCreator;
public static final String OWNER = "1";
public static final String CREATER = "1";
public static final String STUDENT = "student";
public ContactAdapter(Context ct, List<ContactModel.MembersEntity> mLists, int permission, String createrID) {
this.mLists = mLists;
mContext = ct;
mPermission = permission;
this.addAll(mLists);
this.createrID = createrID;
if (createrID.equals(CREATER)) {
isCreator = true;
} else {
isCreator = false;
}
}
@Override
public ContactAdapter.ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_contact, parent, false);
return new ContactViewHolder(view);
}
@Override
public void onBindViewHolder(ContactAdapter.ContactViewHolder holder, final int position) {
SwipeItemLayout swipeRoot = holder.mRoot;
if (getItem(position).getId().equals(OWNER)) {
swipeRoot.setSwipeAble(false);
} else if (isCreator) {
swipeRoot.setSwipeAble(true);
swipeRoot.setDelegate(new SwipeItemLayout.SwipeItemLayoutDelegate() {
@Override
public void onSwipeItemLayoutOpened(SwipeItemLayout swipeItemLayout) {
closeOpenedSwipeItemLayoutWithAnim();
mOpenedSil.add(swipeItemLayout);
}
@Override
public void onSwipeItemLayoutClosed(SwipeItemLayout swipeItemLayout) {
mOpenedSil.remove(swipeItemLayout);
}
@Override
public void onSwipeItemLayoutStartOpen(SwipeItemLayout swipeItemLayout) {
closeOpenedSwipeItemLayoutWithAnim();
}
});
holder.mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) mContext).deleteUser(position);
}
});
} else {
if (mPermission == CommonString.PermissionCode.TEACHER) {
if (position != 0) {
if (getItem(position).getProfession().equals(STUDENT)) {
swipeRoot.setSwipeAble(true);
swipeRoot.setDelegate(new SwipeItemLayout.SwipeItemLayoutDelegate() {
@Override
public void onSwipeItemLayoutOpened(SwipeItemLayout swipeItemLayout) {
closeOpenedSwipeItemLayoutWithAnim();
mOpenedSil.add(swipeItemLayout);
}
@Override
public void onSwipeItemLayoutClosed(SwipeItemLayout swipeItemLayout) {
mOpenedSil.remove(swipeItemLayout);
}
@Override
public void onSwipeItemLayoutStartOpen(SwipeItemLayout swipeItemLayout) {
closeOpenedSwipeItemLayoutWithAnim();
}
});
holder.mDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) mContext).deleteUser(position);
}
});
} else {
swipeRoot.setSwipeAble(false);
}
} else {
swipeRoot.setSwipeAble(false);
}
} else {
swipeRoot.setSwipeAble(false);
}
}
TextView textView = holder.mName;
textView.setText(getItem(position).getUsername());
}
@Override
public long getHeaderId(int position) {
return getItem(position).getSortLetters().charAt(0);
}
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contact_header, parent, false);
return new RecyclerView.ViewHolder(view) {
};
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
TextView textView = (TextView) holder.itemView;
String showValue = String.valueOf(getItem(position).getSortLetters().charAt(0));
if ("$".equals(showValue)) {
textView.setText("群主");
} else if ("%".equals(showValue)) {
textView.setText("系统管理员");
} else {
textView.setText(showValue);
}
}
public int getPositionForSection(char section) {
for (int i = 0; i < getItemCount(); i++) {
String sortStr = mLists.get(i).getSortLetters();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
public void closeOpenedSwipeItemLayoutWithAnim() {
for (SwipeItemLayout sil : mOpenedSil) {
sil.closeWithAnim();
}
mOpenedSil.clear();
}
public class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView mName;
public SwipeItemLayout mRoot;
public TextView mDelete;
public ContactViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.item_contact_title);
mRoot = (SwipeItemLayout) itemView.findViewById(R.id.item_contact_swipe_root);
mDelete = (TextView) itemView.findViewById(R.id.item_contact_delete);
}
}
}