我前段时间遇到过这个问题。贝娄是一个例子(java代码 - 因为它是前一段时间)
我使用了 DelayAutoCompleteTextView 并制作了自定义适配器
私人无效 setAutocompleteForPatientName(ArrayList 患者) {
contactName = (DelayAutoCompleteTextView) findViewById(R.id.nameET);
联系人姓名.setThreshold(1);
contactName.setAdapter(new YourCustomAdapter(this, contacts));
联系人名称.setLoadingIndicator(
(android.widget.ProgressBar) findViewById(R.id.pb_loading_indicator));
contactName.setOnItemClickListener(新的 AdapterView.OnItemClickListener() {
@覆盖
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
DO WHATEVER YOU WANT WITH SELECTION
}
});
}
我的适配器正在实现 Filterable 。
下面是延迟自动完成
公共类 DelayAutoCompleteTextView 扩展 AutoCompleteTextView {
private static final int MESSAGE_TEXT_CHANGED = 100;
private static final int DEFAULT_AUTOCOMPLETE_DELAY = 750;
private int mAutoCompleteDelay = DEFAULT_AUTOCOMPLETE_DELAY;
private ProgressBar mLoadingIndicator;
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
DelayAutoCompleteTextView.super.performFiltering((CharSequence) msg.obj, msg.arg1);
}
};
public DelayAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setLoadingIndicator(ProgressBar progressBar) {
mLoadingIndicator = progressBar;
}
public void setAutoCompleteDelay(int autoCompleteDelay) {
mAutoCompleteDelay = autoCompleteDelay;
}
@Override
protected void performFiltering(CharSequence text, int keyCode) {
if (mLoadingIndicator != null) {
mLoadingIndicator.setVisibility(View.VISIBLE);
}
mHandler.removeMessages(MESSAGE_TEXT_CHANGED);
mHandler.sendMessageDelayed(mHandler.obtainMessage(MESSAGE_TEXT_CHANGED, text), mAutoCompleteDelay);
}
@Override
public void onFilterComplete(int count) {
if (mLoadingIndicator != null) {
mLoadingIndicator.setVisibility(View.GONE);
}
super.onFilterComplete(count);
}
}
这里是xml
<YOURPACKAGE THAT CONTAINS YOUR CUSTOM VIEW CLASS .DelayAutoCompleteTextView
android:id="@+id/nameET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:hint="@string/patientName"
android:imeOptions="flagNoExtractUi|actionSearch"
android:inputType="textCapSentences"
android:paddingRight="20dp"
android:textSize="12sp" />
<ProgressBar
android:id="@+id/pb_loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="20dp"
android:visibility="gone" />
</FrameLayout>