需求分析
要求移动端也要有类似PC输入框一样的,可输可选的一个控件,如
AutoCompleteTextView控件的属性不在这里赘述,可查看 AutoCompleteTextView 控件实现提示文本以及模糊查询_Little__J的博客-CSDN博客
1、布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:gravity="center"
android:text="取消"
android:textColor="@color/black_words"/>
<TextView
android:id="@+id/tv_dialog_line"
style="@style/CommonLineStyle"
android:layout_above="@+id/tv_cancel"/>
<TextView
android:id="@+id/tv_dialog_space"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_above="@+id/tv_dialog_line"
android:background="@color/white_bg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tv_dialog_space"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_searchlay"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="60dp"
android:background="@color/deep_blue">
<AutoCompleteTextView
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/et_white_bg"
android:hint="输入搜索内容"
android:paddingLeft="10dp"
android:textColor="@color/black_words"
android:textSize="14sp"
android:singleLine="true"
android:imeOptions="actionSearch"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
2、自定义adapter,实现Filterable接口
public class CustomerSearchAdapter extends BaseAbstractAdapter<String> implements Filterable {
private ArrayFilter mFilter;
private final Object mLock = new Object();
private List<String> mOriginalValues;
public CustomerSearchAdapter(Context context, List<String> listData, int resId) {
super(context, listData, resId);
}
@Override
public void convert(ViewHolder holder, String bean, int position) {
TextView textView = holder.getView(R.id.tv_content);
textView.setText(bean);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<>(listData);
}
}
if (prefix == null || prefix.length() == 0){
synchronized (mLock) {
ArrayList<String> list = new ArrayList<String>(mOriginalValues); // List<T>
results.values = list;
results.count = list.size();
}
}else {
List<String> OValues = mOriginalValues;
Set<String> newValues = new HashSet<String>(OValues.size());
for (int i = 0; i < OValues.size(); i++) {
String value = OValues.get(i);
if (value.contains(prefix.toString())) { //实现模糊查询
newValues.add(value);
}
}
List<String> list = Set2List(newValues); // 转成List
results.values = list;
results.count = list.size();
}
return results;
}
@SuppressWarnings("unchecked")
protected void publishResults(CharSequence constraint, FilterResults results) {
listData = (List<String>) results.values;
if (results != null && results.count > 0) {
notifyDataSetChanged();
}else {
notifyDataSetInvalidated();
}
}
}
@SuppressWarnings("hiding")
public <T extends Object> List<T> Set2List(Set<T> oSet) {
List<T> tList = new ArrayList<T>(oSet);
// 需要在用到的时候另外写构造,根据需要生成List的对应子类。
return tList;
}
}
3、使用
AutoCompleteTextView edittext = view.findViewById(R.id.et_search);
CustomerSearchAdapter dialogAdapter = new CustomerSearchAdapter(StockFilterActivityFour.this, originalList, R.layout.item_base_dialog);
edittext.setAdapter(dialogAdapter);
参考 https://github.com/liuwan1992/SearchView/blob/master/app/src/main/res/layout/activity_main.xml