Last active
August 29, 2015 14:27
-
-
Save logcat/734488d1e7ec7a30eb29 to your computer and use it in GitHub Desktop.
Based on AppCompatAutoCompleteTextView suggestions
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
import android.content.Context; | |
import android.graphics.Rect; | |
import android.support.v7.widget.AppCompatAutoCompleteTextView; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.AdapterView; | |
// style it with @style/Widget.AppCompat.Spinner.Underlined | |
public class DropDownSpinner extends AppCompatAutoCompleteTextView { | |
private boolean isPopup; | |
public DropDownSpinner(Context context) { | |
super(context); | |
init(); | |
} | |
public DropDownSpinner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public DropDownSpinner(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
super.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
isPopup = false; | |
} | |
}); | |
} | |
@Override | |
public boolean enoughToFilter() { | |
return true; | |
} | |
@Override | |
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { | |
super.onFocusChanged(focused, direction, previouslyFocusedRect); | |
if (focused) { | |
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(getWindowToken(), 0); | |
setKeyListener(null); | |
} else { | |
isPopup = false; | |
} | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (MotionEvent.ACTION_UP == event.getAction()) { | |
if (isPopup) { | |
dismissDropDown(); | |
isPopup = false; | |
} else { | |
requestFocus(); | |
DropDownSpinner.super.showDropDown(); | |
isPopup = true; | |
} | |
} | |
return super.onTouchEvent(event); | |
} | |
@Override | |
public void dismissDropDown() { | |
isPopup = false; | |
super.dismissDropDown(); | |
} | |
@Override | |
public void showDropDown() { | |
// XXX we can show it by click only | |
} | |
@Override | |
public void setOnItemClickListener(AdapterView.OnItemClickListener l) { | |
throw new RuntimeException("OnItemClickListener is used for internal stuff, use OnItemSelectedListener instead"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment