forked from razerdp/BasePopup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListPopup.java
229 lines (188 loc) · 7.18 KB
/
ListPopup.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
package razerdp.demo.popup;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import razerdp.basepopup.BasePopupWindow;
import razerdp.basepopup.R;
/**
* Created by 大灯泡 on 2016/1/20.
* 包含着listview的popup,使用builder模式,事件与tag进行绑定
*/
public class ListPopup extends BasePopupWindow {
private ListView mListView;
private OnListPopupItemClickListener mOnListPopupItemClickListener;
private ListPopup(Context context) {
super(context);
}
private ListPopup(Context context, Builder builder) {
this(context);
mListView = (ListView) findViewById(R.id.popup_list);
setAdapter(context, builder);
setPopupGravity(Gravity.CENTER);
setClipChildren(false);
}
public static class Builder {
private List<Object> mItemEventList = new ArrayList<>();
private Activity mContext;
public Builder(Activity context) {
mContext = context;
}
public Builder addItem(String itemTx) {
mItemEventList.add(itemTx);
return this;
}
public Builder addItem(int clickTag, String itemTx) {
mItemEventList.add(new clickItemEvent(clickTag, itemTx));
return this;
}
public List<Object> getItemEventList() {
return mItemEventList;
}
public ListPopup build() {
return new ListPopup(mContext, this);
}
}
public static class clickItemEvent {
private int clickTag;
private String itemTx;
public clickItemEvent(int clickTag, String itemTx) {
this.clickTag = clickTag;
this.itemTx = itemTx;
}
public int getClickTag() {
return clickTag;
}
public void setClickTag(int clickTag) {
this.clickTag = clickTag;
}
public String getItemTx() {
return itemTx;
}
public void setItemTx(String itemTx) {
this.itemTx = itemTx;
}
}
//=============================================================adapter
class ListPopupAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mContext;
private List<Object> mItemList;
public ListPopupAdapter(Context context, @NonNull List<Object> itemList) {
mContext = context;
mItemList = itemList;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public String getItem(int position) {
if (mItemList.get(position) instanceof String) {
return (String) mItemList.get(position);
}
if (mItemList.get(position) instanceof clickItemEvent) {
return ((clickItemEvent) mItemList.get(position)).getItemTx();
}
return "";
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_popup_list, parent, false);
vh.mTextView = (TextView) convertView.findViewById(R.id.item_tx);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.mTextView.setText(getItem(position));
return convertView;
}
public List<Object> getItemList() {
return this.mItemList;
}
class ViewHolder {
public TextView mTextView;
}
}
//=============================================================init
private void setAdapter(Context context, Builder builder) {
if (builder.getItemEventList() == null || builder.getItemEventList().size() == 0) return;
final ListPopupAdapter adapter = new ListPopupAdapter(context, builder.getItemEventList());
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mOnListPopupItemClickListener != null) {
Object clickObj = adapter.getItemList().get(position);
if (clickObj instanceof String) {
mOnListPopupItemClickListener.onItemClick(position);
}
if (clickObj instanceof clickItemEvent) {
int what = ((clickItemEvent) clickObj).clickTag;
mOnListPopupItemClickListener.onItemClick(what);
}
}
}
});
}
//=============================================================super methods
@Override
protected Animation onCreateShowAnimation() {
return null;
}
@Override
protected Animation onCreateDismissAnimation() {
return null;
}
@Override
public Animator onCreateShowAnimator() {
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(getDisplayAnimateView(), "rotationX", 90f, 0f).setDuration(400),
ObjectAnimator.ofFloat(getDisplayAnimateView(), "translationY", 250f, 0f).setDuration(400),
ObjectAnimator.ofFloat(getDisplayAnimateView(), "alpha", 0f, 1f).setDuration(400 * 3 / 2));
return set;
}
@Override
public View onCreateContentView() {
return createPopupById(R.layout.popup_list);
}
@Override
public Animator onCreateDismissAnimator() {
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(getDisplayAnimateView(), "rotationX", 0f, 90f).setDuration(400),
ObjectAnimator.ofFloat(getDisplayAnimateView(), "translationY", 0f, 250f).setDuration(400),
ObjectAnimator.ofFloat(getDisplayAnimateView(), "alpha", 1f, 0f).setDuration(400 * 3 / 2));
return set;
}
//=============================================================interface
public OnListPopupItemClickListener getOnListPopupItemClickListener() {
return mOnListPopupItemClickListener;
}
public void setOnListPopupItemClickListener(OnListPopupItemClickListener onListPopupItemClickListener) {
mOnListPopupItemClickListener = onListPopupItemClickListener;
}
public interface OnListPopupItemClickListener {
void onItemClick(int what);
}
}