-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathFloatingSearchView.java
708 lines (573 loc) · 25.1 KB
/
FloatingSearchView.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package com.mypopsy.widget;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.MenuRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.view.MarginLayoutParamsCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Xml;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.mypopsy.floatingsearchview.R;
import com.mypopsy.widget.internal.RoundRectDrawableWithShadow;
import com.mypopsy.widget.internal.SuggestionItemDecorator;
import com.mypopsy.widget.internal.ViewUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static com.mypopsy.widget.internal.RoundRectDrawableWithShadow.BOTTOM;
import static com.mypopsy.widget.internal.RoundRectDrawableWithShadow.LEFT;
import static com.mypopsy.widget.internal.RoundRectDrawableWithShadow.RIGHT;
import static com.mypopsy.widget.internal.RoundRectDrawableWithShadow.TOP;
public class FloatingSearchView extends RelativeLayout {
private static final int DEFAULT_BACKGROUND_COLOR = 0x90000000;
private static final int DEFAULT_CONTENT_COLOR = 0xfff0f0f0;
private static final int DEFAULT_RADIUS = 2;
private static final int DEFAULT_ELEVATION = 2;
private static final int DEFAULT_MAX_ELEVATION = 2;
private static final long DEFAULT_DURATION_ENTER = 300;
private static final long DEFAULT_DURATION_EXIT = 400;
private static final Interpolator DECELERATE = new DecelerateInterpolator(3f);
private static final Interpolator ACCELERATE = new AccelerateInterpolator(2f);
private RecyclerView.AdapterDataObserver mAdapterObserver = new android.support.v7.widget.RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
onChanged();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
onChanged();
}
@Override
public void onChanged() {
updateSuggestionsVisibility();
}
};
public interface OnSearchListener {
void onSearchAction(CharSequence text);
}
public interface OnIconClickListener {
void onNavigationClick();
}
public interface OnSearchFocusChangedListener {
void onFocusChanged(boolean focused);
}
final private LogoEditText mSearchInput;
final private ImageView mNavButtonView;
final private RecyclerView mRecyclerView;
final private ViewGroup mSearchContainer;
final private View mDivider;
final private ActionMenuView mActionMenu;
final private Activity mActivity;
final private RoundRectDrawableWithShadow mSearchBackground;
final private SuggestionItemDecorator mCardDecorator;
final private List<Integer> mAlwaysShowingMenu = new ArrayList<>();
private OnSearchFocusChangedListener mFocusListener;
private OnIconClickListener mNavigationClickListener;
private Drawable mBackgroundDrawable;
private boolean mSuggestionsShown;
public FloatingSearchView(Context context) {
this(context, null);
}
public FloatingSearchView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.floatingSearchViewStyle);
}
public FloatingSearchView(Context context, AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (isInEditMode()) {
mActivity = null;
} else {
mActivity = getActivity();
}
setFocusable(true);
setFocusableInTouchMode(true);
inflate(getContext(), R.layout.fsv_floating_search_layout, this);
mSearchInput = (LogoEditText)findViewById(R.id.fsv_search_text);
mNavButtonView = (ImageView) findViewById(R.id.fsv_search_action_navigation);
mRecyclerView = (RecyclerView) findViewById(R.id.fsv_suggestions_list);
mDivider = findViewById(R.id.fsv_suggestions_divider);
mSearchContainer = (ViewGroup) findViewById(R.id.fsv_search_container);
mActionMenu = (ActionMenuView) findViewById(R.id.fsv_search_action_menu);
//TODO: move elevation parameters to XML attributes
mSearchBackground = new RoundRectDrawableWithShadow(
DEFAULT_CONTENT_COLOR, ViewUtils.dpToPx(DEFAULT_RADIUS),
ViewUtils.dpToPx(DEFAULT_ELEVATION),
ViewUtils.dpToPx(DEFAULT_MAX_ELEVATION));
mSearchBackground.setAddPaddingForCorners(true);
mCardDecorator = new SuggestionItemDecorator(mSearchBackground.mutate());
applyXmlAttributes(attrs, defStyleAttr, 0);
setupViews();
}
private void applyXmlAttributes(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
final TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.FloatingSearchView, defStyleAttr, defStyleRes);
// Search bar width
View suggestionsContainer = findViewById(R.id.fsv_suggestions_container);
int searchBarWidth = a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_searchBarWidth,
mSearchContainer.getLayoutParams().width);
mSearchContainer.getLayoutParams().width = searchBarWidth;
suggestionsContainer.getLayoutParams().width = searchBarWidth;
// Divider
mDivider.setBackgroundDrawable(a.getDrawable(R.styleable.FloatingSearchView_android_divider));
int dividerHeight = a.getDimensionPixelSize(R.styleable.FloatingSearchView_android_dividerHeight, -1);
MarginLayoutParams dividerLP = (MarginLayoutParams) mDivider.getLayoutParams();
if(mDivider.getBackground() != null && dividerHeight != -1)
dividerLP.height = dividerHeight;
float maxShadowSize = mSearchBackground.getMaxShadowSize();
float cornerRadius = mSearchBackground.getCornerRadius();
int horizontalPadding = (int) (RoundRectDrawableWithShadow.calculateHorizontalPadding(
maxShadowSize, cornerRadius, false) + .5f);
dividerLP.setMargins(horizontalPadding, dividerLP.topMargin, horizontalPadding, dividerLP.bottomMargin);
mDivider.setLayoutParams(dividerLP);
// Content inset
MarginLayoutParams searchParams = (MarginLayoutParams) mSearchInput.getLayoutParams();
int contentInsetStart = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetStart,
MarginLayoutParamsCompat.getMarginStart(searchParams));
int contentInsetEnd = a.getDimensionPixelSize(R.styleable.FloatingSearchView_contentInsetEnd,
MarginLayoutParamsCompat.getMarginEnd(searchParams));
MarginLayoutParamsCompat.setMarginStart(searchParams, contentInsetStart);
MarginLayoutParamsCompat.setMarginEnd(searchParams, contentInsetEnd);
// anything else
setLogo(a.getDrawable(R.styleable.FloatingSearchView_logo));
setContentBackgroundColor(a.getColor(R.styleable.FloatingSearchView_fsv_contentBackgroundColor, DEFAULT_CONTENT_COLOR));
setRadius(a.getDimensionPixelSize(R.styleable.FloatingSearchView_fsv_cornerRadius, ViewUtils.dpToPx(DEFAULT_RADIUS)));
inflateMenu(a.getResourceId(R.styleable.FloatingSearchView_fsv_menu, 0));
setPopupTheme(a.getResourceId(R.styleable.FloatingSearchView_popupTheme, 0));
setHint(a.getString(R.styleable.FloatingSearchView_android_hint));
setIcon(a.getDrawable(R.styleable.FloatingSearchView_fsv_icon));
a.recycle();
}
private void setupViews() {
mSearchContainer.setLayoutTransition(getDefaultLayoutTransition());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mSearchContainer.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
mSearchContainer.setBackgroundDrawable(mSearchBackground);
mSearchContainer.setMinimumHeight((int) mSearchBackground.getMinHeight());
mSearchContainer.setMinimumWidth((int) mSearchBackground.getMinWidth());
mRecyclerView.addItemDecoration(mCardDecorator);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setVisibility(View.INVISIBLE);
mBackgroundDrawable = getBackground();
if(mBackgroundDrawable != null)
mBackgroundDrawable = mBackgroundDrawable.mutate();
else
mBackgroundDrawable = new ColorDrawable(DEFAULT_BACKGROUND_COLOR);
setBackgroundDrawable(mBackgroundDrawable);
mBackgroundDrawable.setAlpha(0);
mNavButtonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mNavigationClickListener != null)
mNavigationClickListener.onNavigationClick();
}
});
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!isActivated()) return false;
setActivated(false);
return true;
}
});
mSearchInput.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus != isActivated()) setActivated(hasFocus);
}
});
mSearchInput.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode != KeyEvent.KEYCODE_ENTER) return false;
setActivated(false);
return true;
}
});
}
public void setRadius(float radius) {
mSearchBackground.setCornerRadius(radius);
mCardDecorator.setCornerRadius(radius);
}
public void setContentBackgroundColor(@ColorInt int color) {
mSearchBackground.setColor(color);
mCardDecorator.setBackgroundColor(color);
mActionMenu.setBackgroundColor(color);
}
public Menu getMenu() {
return mActionMenu.getMenu();
}
public void setPopupTheme(@StyleRes int resId) {
mActionMenu.setPopupTheme(resId);
}
public void inflateMenu(@MenuRes int menuRes) {
if(menuRes == 0) return;
if (isInEditMode()) return;
getActivity().getMenuInflater().inflate(menuRes, mActionMenu.getMenu());
XmlResourceParser parser = null;
try {
//noinspection ResourceType
parser = getResources().getLayout(menuRes);
AttributeSet attrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs);
} catch (XmlPullParserException | IOException e) {
// should not happens
throw new InflateException("Error parsing menu XML", e);
} finally {
if (parser != null) parser.close();
}
}
public void setOnSearchListener(final OnSearchListener listener) {
mSearchInput.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode != KeyEvent.KEYCODE_ENTER) return false;
listener.onSearchAction(mSearchInput.getText());
return true;
}
});
}
public void setOnMenuItemClickListener(ActionMenuView.OnMenuItemClickListener listener) {
mActionMenu.setOnMenuItemClickListener(listener);
}
public CharSequence getText() {
return mSearchInput.getText();
}
public void setText(CharSequence text) {
mSearchInput.setText(text);
}
public void setHint(CharSequence hint) {
mSearchInput.setHint(hint);
}
@Override
public void setActivated(boolean activated) {
if(activated == isActivated()) return;
super.setActivated(activated);
if(activated) {
mSearchInput.requestFocus();
ViewUtils.showSoftKeyboardDelayed(mSearchInput, 100);
}else {
requestFocus();
ViewUtils.closeSoftKeyboard(mActivity);
}
if(mFocusListener != null)
mFocusListener.onFocusChanged(activated);
showMenu(!activated);
fadeIn(activated);
updateSuggestionsVisibility();
}
public void setOnIconClickListener(OnIconClickListener navigationClickListener) {
mNavigationClickListener = navigationClickListener;
}
public void setOnSearchFocusChangedListener(OnSearchFocusChangedListener focusListener) {
mFocusListener = focusListener;
}
public void addTextChangedListener(TextWatcher textWatcher) {
mSearchInput.addTextChangedListener(textWatcher);
}
public void removeTextChangedListener(TextWatcher textWatcher) {
mSearchInput.removeTextChangedListener(textWatcher);
}
public void setAdapter(RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter) {
RecyclerView.Adapter<? extends RecyclerView.ViewHolder> old = getAdapter();
if(old != null) old.unregisterAdapterDataObserver(mAdapterObserver);
adapter.registerAdapterDataObserver(mAdapterObserver);
mRecyclerView.setAdapter(adapter);
}
public void setItemAnimator(RecyclerView.ItemAnimator itemAnimator) {
mRecyclerView.setItemAnimator(itemAnimator);
}
public void addItemDecoration(RecyclerView.ItemDecoration decoration) {
mRecyclerView.addItemDecoration(decoration);
}
public void setLogo(Drawable drawable) {
mSearchInput.setLogo(drawable);
}
public void setLogo(@DrawableRes int resId) {
mSearchInput.setLogo(resId);
}
public void setIcon(@DrawableRes int resId) {
showIcon(resId != 0);
mNavButtonView.setImageResource(resId);
}
public void setIcon(Drawable drawable) {
showIcon(drawable != null);
mNavButtonView.setImageDrawable(drawable);
}
public void showLogo(boolean show) {
mSearchInput.showLogo(show);
}
public void showIcon(boolean show) {
mNavButtonView.setVisibility(show ? View.VISIBLE : View.GONE);
}
public Drawable getIcon() {
if(mNavButtonView == null) return null;
return mNavButtonView.getDrawable();
}
@SuppressWarnings("unchecked")
@Nullable
public RecyclerView.Adapter<? extends RecyclerView.ViewHolder> getAdapter() {
return mRecyclerView.getAdapter();
}
protected LayoutTransition getDefaultLayoutTransition() {
return new LayoutTransition();
}
private void fadeIn(boolean enter) {
ValueAnimator backgroundAnim;
if(Build.VERSION.SDK_INT >= 19)
backgroundAnim = ObjectAnimator.ofInt(mBackgroundDrawable, "alpha", enter ? 255 : 0);
else {
backgroundAnim = ValueAnimator.ofInt(enter ? 0 : 255, enter ? 255 : 0);
backgroundAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
mBackgroundDrawable.setAlpha(value);
}
});
}
backgroundAnim.setDuration(enter ? DEFAULT_DURATION_ENTER : DEFAULT_DURATION_EXIT);
backgroundAnim.setInterpolator(enter ? DECELERATE : ACCELERATE);
backgroundAnim.start();
Drawable icon = unwrap(getIcon());
if(icon != null) {
ObjectAnimator iconAnim = ObjectAnimator.ofFloat(icon, "progress", enter ? 1 : 0);
iconAnim.setDuration(backgroundAnim.getDuration());
iconAnim.setInterpolator(backgroundAnim.getInterpolator());
iconAnim.start();
}
}
private int getSuggestionsCount() {
RecyclerView.Adapter<? extends RecyclerView.ViewHolder> adapter = getAdapter();
if(adapter == null) return 0;
return adapter.getItemCount();
}
private void updateSuggestionsVisibility() {
showSuggestions(isActivated() && getSuggestionsCount() > 0);
}
private boolean suggestionsShown() {
return mSuggestionsShown;
}
private void showSuggestions(final boolean show) {
if(show == suggestionsShown()) return;
mSuggestionsShown = show;
int childCount = mRecyclerView.getChildCount();
int translation = 0;
final Runnable endAction = new Runnable() {
@Override
public void run() {
if(show)
updateDivider();
else {
showDivider(false);
mRecyclerView.setVisibility(View.INVISIBLE);
mRecyclerView.setTranslationY(-mRecyclerView.getHeight());
}
}
};
if(show) {
updateDivider();
mRecyclerView.setVisibility(VISIBLE);
if(mRecyclerView.getTranslationY() == 0)
mRecyclerView.setTranslationY(-mRecyclerView.getHeight());
}else if(childCount > 0)
translation = -mRecyclerView.getChildAt(childCount - 1).getBottom();
else
showDivider(false);
ViewPropertyAnimatorCompat listAnim = ViewCompat.animate(mRecyclerView)
.translationY(translation)
.setDuration(show ? DEFAULT_DURATION_ENTER : DEFAULT_DURATION_EXIT)
.setInterpolator(show ? DECELERATE : ACCELERATE)
.withLayer()
.withEndAction(endAction);
if(show || childCount > 0)
listAnim.start();
else
endAction.run();
}
private void showDivider(boolean visible) {
mDivider.setVisibility(visible ? View.VISIBLE : View.GONE);
int shadows = TOP|LEFT|RIGHT;
if(!visible) shadows|=BOTTOM;
mSearchBackground.setShadow(shadows);
}
private void updateDivider() {
showDivider(isActivated() && getSuggestionsCount() > 0);
}
@NonNull
private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity)context;
}
context = ((ContextWrapper)context).getBaseContext();
}
throw new IllegalStateException();
}
private void showMenu(final boolean visible) {
Menu menu = getMenu();
for(int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if(mAlwaysShowingMenu.contains(item.getItemId())) continue;
item.setVisible(visible);
}
}
private void parseMenu(XmlPullParser parser, AttributeSet attrs)
throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
String tagName;
boolean lookingForEndOfUnknownTag = false;
String unknownTagName = null;
// This loop will skip to the menu start tag
do {
if (eventType == XmlPullParser.START_TAG) {
tagName = parser.getName();
if (tagName.equals("menu")) {
// Go to next tag
eventType = parser.next();
break;
}
throw new RuntimeException("Expecting menu, got " + tagName);
}
eventType = parser.next();
} while (eventType != XmlPullParser.END_DOCUMENT);
boolean reachedEndOfMenu = false;
while (!reachedEndOfMenu) {
switch (eventType) {
case XmlPullParser.START_TAG:
if (lookingForEndOfUnknownTag) {
break;
}
tagName = parser.getName();
if (tagName.equals("item")) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MenuItem);
int itemShowAsAction = a.getInt(R.styleable.MenuItem_showAsAction, -1);
if((itemShowAsAction & MenuItem.SHOW_AS_ACTION_ALWAYS) != 0) {
int itemId = a.getResourceId(R.styleable.MenuItem_android_id, NO_ID);
if(itemId != NO_ID) mAlwaysShowingMenu.add(itemId);
}
a.recycle();
} else {
lookingForEndOfUnknownTag = true;
unknownTagName = tagName;
}
break;
case XmlPullParser.END_TAG:
tagName = parser.getName();
if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
lookingForEndOfUnknownTag = false;
unknownTagName = null;
} else if (tagName.equals("menu")) {
reachedEndOfMenu = true;
}
break;
case XmlPullParser.END_DOCUMENT:
throw new RuntimeException("Unexpected end of document");
}
eventType = parser.next();
}
}
static private Drawable unwrap(Drawable icon) {
if(icon instanceof android.support.v7.graphics.drawable.DrawableWrapper)
return ((android.support.v7.graphics.drawable.DrawableWrapper)icon).getWrappedDrawable();
if(icon instanceof android.support.v4.graphics.drawable.DrawableWrapper)
return ((android.support.v4.graphics.drawable.DrawableWrapper)icon).getWrappedDrawable();
if(Build.VERSION.SDK_INT >= 23 && icon instanceof android.graphics.drawable.DrawableWrapper)
return ((android.graphics.drawable.DrawableWrapper)icon).getDrawable();
return DrawableCompat.unwrap(icon);
}
private static class RecyclerView extends android.support.v7.widget.RecyclerView {
public RecyclerView(Context context) {
super(context);
}
public RecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
View child = findChildViewUnder(e.getX(), e.getY());
return child != null && super.onTouchEvent(e);
}
}
private static class LogoEditText extends AppCompatEditText {
private Drawable logo;
private boolean logoShown;
private boolean dirty;
public LogoEditText(Context context) {
super(context);
}
public LogoEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LogoEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void showLogo(boolean shown) {
logoShown = shown;
}
public void setLogo(@DrawableRes int res) {
if(res == 0)
setLogo(null);
else
setLogo(ResourcesCompat.getDrawable(getResources(), res, getContext().getTheme()));
}
public void setLogo(Drawable logo) {
this.logo = logo;
dirty = true;
}
@Override
protected void onDraw(Canvas canvas) {
if(logoShown && logo != null) {
if(dirty) {
updateLogoBounds();
dirty = false;
}
logo.draw(canvas);
}else
super.onDraw(canvas);
}
// fit center
private void updateLogoBounds() {
int logoHeight = Math.min(getHeight(), logo.getIntrinsicHeight());
int top = (getHeight() - logoHeight)/2;
int logoWidth = (logo.getIntrinsicWidth()*logoHeight)/logo.getIntrinsicHeight();
logo.setBounds(0, top, logoWidth, top + logoHeight);
}
}
}