-
Notifications
You must be signed in to change notification settings - Fork 645
/
FilterView.java
407 lines (360 loc) · 14.7 KB
/
FilterView.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package com.sunfusheng.StickyHeaderListView.view;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.sunfusheng.StickyHeaderListView.R;
import com.sunfusheng.StickyHeaderListView.adapter.FilterLeftAdapter;
import com.sunfusheng.StickyHeaderListView.adapter.FilterOneAdapter;
import com.sunfusheng.StickyHeaderListView.adapter.FilterRightAdapter;
import com.sunfusheng.StickyHeaderListView.model.FilterData;
import com.sunfusheng.StickyHeaderListView.model.FilterEntity;
import com.sunfusheng.StickyHeaderListView.model.FilterTwoEntity;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by sunfusheng on 16/4/20.
*/
public class FilterView extends LinearLayout implements View.OnClickListener {
@BindView(R.id.tv_category_title)
TextView tvCategoryTitle;
@BindView(R.id.iv_category_arrow)
ImageView ivCategoryArrow;
@BindView(R.id.tv_sort_title)
TextView tvSortTitle;
@BindView(R.id.iv_sort_arrow)
ImageView ivSortArrow;
@BindView(R.id.tv_filter_title)
TextView tvFilterTitle;
@BindView(R.id.iv_filter_arrow)
ImageView ivFilterArrow;
@BindView(R.id.ll_category)
LinearLayout llCategory;
@BindView(R.id.ll_sort)
LinearLayout llSort;
@BindView(R.id.ll_filter)
LinearLayout llFilter;
@BindView(R.id.lv_left)
ListView lvLeft;
@BindView(R.id.lv_right)
ListView lvRight;
@BindView(R.id.ll_head_layout)
LinearLayout llHeadLayout;
@BindView(R.id.ll_content_list_view)
LinearLayout llContentListView;
@BindView(R.id.view_mask_bg)
View viewMaskBg;
private Context mContext;
private Activity mActivity;
private int filterPosition = -1;
private int lastFilterPosition = -1;
public static final int POSITION_CATEGORY = 0; // 分类的位置
public static final int POSITION_SORT = 1; // 排序的位置
public static final int POSITION_FILTER = 2; // 筛选的位置
private boolean isShowing = false;
private int panelHeight;
private FilterData filterData;
private FilterLeftAdapter leftAdapter;
private FilterRightAdapter rightAdapter;
private FilterOneAdapter sortAdapter;
private FilterOneAdapter filterAdapter;
private FilterTwoEntity leftSelectedCategoryEntity; // 被选择的分类项左侧数据
private FilterEntity rightSelectedCategoryEntity; // 被选择的分类项右侧数据
private FilterEntity selectedSortEntity; // 被选择的排序项
private FilterEntity selectedFilterEntity; // 被选择的筛选项
public FilterView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public FilterView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
this.mContext = context;
View view = LayoutInflater.from(context).inflate(R.layout.view_filter_layout, this);
ButterKnife.bind(this, view);
initView();
initListener();
}
private void initView() {
viewMaskBg.setVisibility(GONE);
llContentListView.setVisibility(GONE);
}
private void initListener() {
llCategory.setOnClickListener(this);
llSort.setOnClickListener(this);
llFilter.setOnClickListener(this);
viewMaskBg.setOnClickListener(this);
llContentListView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ll_category:
filterPosition = 0;
if (onFilterClickListener != null) {
onFilterClickListener.onFilterClick(filterPosition);
}
break;
case R.id.ll_sort:
filterPosition = 1;
if (onFilterClickListener != null) {
onFilterClickListener.onFilterClick(filterPosition);
}
break;
case R.id.ll_filter:
filterPosition = 2;
if (onFilterClickListener != null) {
onFilterClickListener.onFilterClick(filterPosition);
}
break;
case R.id.view_mask_bg:
hide();
break;
}
}
// 复位筛选的显示状态
public void resetViewStatus() {
tvCategoryTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow);
tvSortTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivSortArrow.setImageResource(R.mipmap.home_down_arrow);
tvFilterTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_2));
ivFilterArrow.setImageResource(R.mipmap.home_down_arrow);
}
// 复位所有的状态
public void resetAllStatus() {
resetViewStatus();
hide();
}
// 设置分类数据
private void setCategoryAdapter() {
lvLeft.setVisibility(VISIBLE);
lvRight.setVisibility(VISIBLE);
// 左边列表视图
leftAdapter = new FilterLeftAdapter(mContext, filterData.getCategory());
lvLeft.setAdapter(leftAdapter);
if (leftSelectedCategoryEntity == null) {
leftSelectedCategoryEntity = filterData.getCategory().get(0);
}
leftAdapter.setSelectedEntity(leftSelectedCategoryEntity);
lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
leftSelectedCategoryEntity = filterData.getCategory().get(position);
leftAdapter.setSelectedEntity(leftSelectedCategoryEntity);
// 右边列表视图
rightAdapter = new FilterRightAdapter(mContext, leftSelectedCategoryEntity.getList());
lvRight.setAdapter(rightAdapter);
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
}
});
// 右边列表视图
rightAdapter = new FilterRightAdapter(mContext, leftSelectedCategoryEntity.getList());
lvRight.setAdapter(rightAdapter);
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
rightSelectedCategoryEntity = leftSelectedCategoryEntity.getList().get(position);
rightAdapter.setSelectedEntity(rightSelectedCategoryEntity);
if (onItemCategoryClickListener != null) {
onItemCategoryClickListener.onItemCategoryClick(leftSelectedCategoryEntity, rightSelectedCategoryEntity);
}
hide();
}
});
}
// 设置排序数据
private void setSortAdapter() {
lvLeft.setVisibility(GONE);
lvRight.setVisibility(VISIBLE);
sortAdapter = new FilterOneAdapter(mContext, filterData.getSorts());
lvRight.setAdapter(sortAdapter);
lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedSortEntity = filterData.getSorts().get(position);
sortAdapter.setSelectedEntity(selectedSortEntity);
if (onItemSortClickListener != null) {
onItemSortClickListener.onItemSortClick(selectedSortEntity);
}
hide();
}
});
}
// 设置筛选数据
private void setFilterAdapter() {
lvLeft.setVisibility(GONE);
lvRight.setVisibility(VISIBLE);
filterAdapter = new FilterOneAdapter(mContext, filterData.getFilters());
lvRight.setAdapter(filterAdapter);
lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedFilterEntity = filterData.getFilters().get(position);
filterAdapter.setSelectedEntity(selectedFilterEntity);
if (onItemFilterClickListener != null) {
onItemFilterClickListener.onItemFilterClick(selectedFilterEntity);
}
hide();
}
});
}
// 动画显示
public void show(int position) {
if (isShowing && lastFilterPosition == position) {
hide();
return;
} else if (!isShowing) {
viewMaskBg.setVisibility(VISIBLE);
llContentListView.setVisibility(VISIBLE);
llContentListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
llContentListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
panelHeight = llContentListView.getHeight();
ObjectAnimator.ofFloat(llContentListView, "translationY", -panelHeight, 0).setDuration(200).start();
}
});
}
isShowing = true;
resetViewStatus();
rotateArrowUp(position);
rotateArrowDown(lastFilterPosition);
lastFilterPosition = position;
switch (position) {
case POSITION_CATEGORY:
tvCategoryTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivCategoryArrow.setImageResource(R.mipmap.home_down_arrow_red);
setCategoryAdapter();
break;
case POSITION_SORT:
tvSortTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivSortArrow.setImageResource(R.mipmap.home_down_arrow_red);
setSortAdapter();
break;
case POSITION_FILTER:
tvFilterTitle.setTextColor(mActivity.getResources().getColor(R.color.colorPrimary));
ivFilterArrow.setImageResource(R.mipmap.home_down_arrow_red);
setFilterAdapter();
break;
}
}
// 隐藏动画
public void hide() {
isShowing = false;
resetViewStatus();
rotateArrowDown(filterPosition);
rotateArrowDown(lastFilterPosition);
filterPosition = -1;
lastFilterPosition = -1;
viewMaskBg.setVisibility(View.GONE);
ObjectAnimator.ofFloat(llContentListView, "translationY", 0, -panelHeight).setDuration(200).start();
}
// 设置筛选数据
public void setFilterData(Activity activity, FilterData filterData) {
this.mActivity = activity;
this.filterData = filterData;
}
// 是否显示
public boolean isShowing() {
return isShowing;
}
public int getFilterPosition() {
return filterPosition;
}
// 旋转箭头向上
private void rotateArrowUp(int position) {
switch (position) {
case POSITION_CATEGORY:
rotateArrowUpAnimation(ivCategoryArrow);
break;
case POSITION_SORT:
rotateArrowUpAnimation(ivSortArrow);
break;
case POSITION_FILTER:
rotateArrowUpAnimation(ivFilterArrow);
break;
}
}
// 旋转箭头向下
private void rotateArrowDown(int position) {
switch (position) {
case POSITION_CATEGORY:
rotateArrowDownAnimation(ivCategoryArrow);
break;
case POSITION_SORT:
rotateArrowDownAnimation(ivSortArrow);
break;
case POSITION_FILTER:
rotateArrowDownAnimation(ivFilterArrow);
break;
}
}
// 旋转箭头向上
public static void rotateArrowUpAnimation(final ImageView iv) {
if (iv == null) return;
RotateAnimation animation = new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(200);
animation.setFillAfter(true);
iv.startAnimation(animation);
}
// 旋转箭头向下
public static void rotateArrowDownAnimation(final ImageView iv) {
if (iv == null) return;
RotateAnimation animation = new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(200);
animation.setFillAfter(true);
iv.startAnimation(animation);
}
// 筛选视图点击
private OnFilterClickListener onFilterClickListener;
public void setOnFilterClickListener(OnFilterClickListener onFilterClickListener) {
this.onFilterClickListener = onFilterClickListener;
}
public interface OnFilterClickListener {
void onFilterClick(int position);
}
// 分类Item点击
private OnItemCategoryClickListener onItemCategoryClickListener;
public void setOnItemCategoryClickListener(OnItemCategoryClickListener onItemCategoryClickListener) {
this.onItemCategoryClickListener = onItemCategoryClickListener;
}
public interface OnItemCategoryClickListener {
void onItemCategoryClick(FilterTwoEntity leftEntity, FilterEntity rightEntity);
}
// 排序Item点击
private OnItemSortClickListener onItemSortClickListener;
public void setOnItemSortClickListener(OnItemSortClickListener onItemSortClickListener) {
this.onItemSortClickListener = onItemSortClickListener;
}
public interface OnItemSortClickListener {
void onItemSortClick(FilterEntity entity);
}
// 筛选Item点击
private OnItemFilterClickListener onItemFilterClickListener;
public void setOnItemFilterClickListener(OnItemFilterClickListener onItemFilterClickListener) {
this.onItemFilterClickListener = onItemFilterClickListener;
}
public interface OnItemFilterClickListener {
void onItemFilterClick(FilterEntity entity);
}
}