Created
October 11, 2016 14:10
-
-
Save afeozzz/65094e9249b11eba7509e68e3399ee05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DelayAutoCompleteTextView extends AutoCompleteTextView { | |
private static final int MESSAGE_TEXT_CHANGED = 100; | |
private static final int DEFAULT_AUTOCOMPLETE_DELAY = 1000; | |
private int mAutoCompleteDelay = DEFAULT_AUTOCOMPLETE_DELAY; | |
private static class AvoidingMemoryLeakHandler extends Handler { | |
private final WeakReference<DelayAutoCompleteTextView> delayAutoCompleteTextViewWeakReference; | |
public AvoidingMemoryLeakHandler(DelayAutoCompleteTextView activity) { | |
delayAutoCompleteTextViewWeakReference = new WeakReference<DelayAutoCompleteTextView>(activity); | |
} | |
@Override | |
public void handleMessage(Message msg) { | |
DelayAutoCompleteTextView delayAutoCompleteTextView = delayAutoCompleteTextViewWeakReference.get(); | |
if (delayAutoCompleteTextView != null && msg != null) { | |
delayAutoCompleteTextView.filter((CharSequence) msg.obj, msg.arg1); | |
} | |
} | |
} | |
private final AvoidingMemoryLeakHandler mHandler = new AvoidingMemoryLeakHandler(this); | |
public DelayAutoCompleteTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public void setAutoCompleteDelay(int autoCompleteDelay) { | |
mAutoCompleteDelay = autoCompleteDelay; | |
} | |
long lastTime = 0; | |
@Override | |
protected void performFiltering(CharSequence text, int keyCode) { | |
mHandler.removeMessages(MESSAGE_TEXT_CHANGED); | |
long curTime = System.currentTimeMillis(); | |
if(curTime - lastTime > mAutoCompleteDelay) { | |
mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_TEXT_CHANGED, text)); | |
} else { | |
mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_TEXT_CHANGED, text), mAutoCompleteDelay - (curTime - lastTime)); | |
} | |
lastTime = System.currentTimeMillis(); | |
} | |
protected void filter(CharSequence text, int keyCode) { | |
DelayAutoCompleteTextView.super.performFiltering(text, keyCode); | |
} | |
@Override | |
public void onFilterComplete(int count) { | |
super.onFilterComplete(count); | |
} | |
@Override | |
public boolean enoughToFilter() { | |
return true; | |
} | |
@Override | |
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { | |
super.onFocusChanged(focused, direction, previouslyFocusedRect); | |
if(focused && !((AddPostActivity)getContext()).isFinishing()) { | |
performFiltering(getText(), 0); | |
showDropDown(); | |
} | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
if (mHandler != null) { | |
mHandler.removeCallbacksAndMessages(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment