需求如题
话不多说 关门放码
《布局》
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/tv_str_service_name"
android:completionThreshold="1"
android:dropDownWidth="100dp"
android:dropDownVerticalOffset="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_marginEnd="16dp"
android:hint="@string/str_service_name_hint"
android:textColorHint="@color/color_979AA2"
android:textColor="@color/color_979AA2"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="@id/tv3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv3" />
关键属性
completionThreshold
代表输入几个字符匹配一次
dropDownWidth/Height
下拉框高度宽度
dropDownVerticalOffset
下拉框与输入框之间的垂直位移
《适配器》
public class AutoCompleteTextAdapter extends BaseAdapter implements Filterable {
public static final int MAX_COUNT = 5;
private ArrayFilter mFilter;//数据过滤器
private List<HistorySearchEntity> mList;//传进来的数据
private ArrayList<HistorySearchEntity> mFilteredData;//
private Context context;//上下文
public static List<HistorySearchEntity> newvalues;
public AutoCompleteTextAdapter(List<HistorySearchEntity> mList, Context context) {
this.mList = mList;
this.context = context;
}
@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
if (convertView == null) {
view = View.inflate(context, R.layout.item_pop_list, null);
viewHolder = new ViewHolder();
viewHolder.tv_city = view.findViewById(R.id.tv_item);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
HistorySearchEntity resultlist = mList.get(position);
viewHolder.tv_city.setText(resultlist.getContent());
return view;
}
static class ViewHolder {
public TextView tv_city;
}
/**
* 在后台线程执行,定义过滤算法
* getFilter()方法会返回一个Filter对象,Filter是一个数据过滤器,其过滤操作是通过performFiltering()方法和publishResult()方法完成的。
* performFiltering()方法进行过滤操作,publishResult()方法用于发表过滤操作结果。
*
* @return
*/
@Override
public Filter getFilter() {
//创建过滤器的对象
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
//过滤器类
class ArrayFilter extends Filter {
/**
* @param constraint:用户在输入框中所输入的内容
* @return
*/
@Override//进行过滤操作
protected FilterResults performFiltering(CharSequence constraint) {
//使用过滤操作的结果
FilterResults results = new FilterResults();
if (mFilteredData == null) {
//以一个集合初始化mFilteredData=new ArrayList<String>(mList);//mList(数组容量)为集合
mFilteredData = new ArrayList<>(mList);
}
//如果没有输入内容,则不过滤
if (constraint == null || constraint.length() == 0) {
ArrayList<HistorySearchEntity> list = mFilteredData;
results.values = list;
results.count = list.size();
} else {
//过滤的条件
String constraintString = constraint.toString().toLowerCase();
//将传进来的全部数据赋值给filteredValues
ArrayList<HistorySearchEntity> filteredValues = mFilteredData;
int count = filteredValues.size();
newvalues = new ArrayList<>(count);
HistorySearchDaoUtil.search(constraintString, (ResultCallBack<List<HistorySearchEntity>>) list -> {
Log.i("damaris", "result: " + GsonUtils.toJson(list));
if (list.size() > MAX_COUNT) {
List<HistorySearchEntity> list1 = new ArrayList<>();
for (int i = 0; i < MAX_COUNT; i++) {
list1.add(list.get(i));
}
newvalues = list1;
results.values = list1;
results.count = list1.size();
} else {
newvalues = list;
results.values = list;
results.count = list.size();
}
});
}
return results;
}
@Override//发表过滤操作结果
protected void publishResults(CharSequence constraint, FilterResults results) {
mList = (List<HistorySearchEntity>) results.values;
if (results.count > 0) {
//重绘当前可见区域
notifyDataSetChanged();
} else {
//重绘控件,还原到初始状态
notifyDataSetInvalidated();
}
}
}
}
过滤条件自己定义。
《Room数据库查询语句》
@Query("SELECT * FROM TABLE_HISTORY_SEARCH_RECORD WHERE content LIKE '%' || :content || '%'")
List<HistorySearchEntity> search(String content);
此处关于Room数据库不做过多讲解,只贴关键代码
《使用》
AppCompatAutoCompleteTextView tvStrServiceName = binding.tvStrServiceName;
tvStrServiceName.setDropDownBackgroundResource(R.color.black);
HistorySearchDaoUtil.getList((ResultCallBack<List<HistorySearchEntity>>) list -> {
AutoCompleteTextAdapter adapter = new AutoCompleteTextAdapter(list, BindRoleDetailActivity.this);
tvStrServiceName.setAdapter(adapter);
});
tvStrServiceName.setOnItemClickListener((parent, view, position, id) -> {
//获取到过滤后的结果的集合
List<HistorySearchEntity> results = AutoCompleteTextAdapter.newvalues;
if (results != null && results.size() > 0) {
HistorySearchEntity result = results.get(position);
String s = result.getContent();
tvStrServiceName.setText(s);
//将光标放到文本最后
tvStrServiceName.setSelection(tvStrServiceName.getText().length());
} else {
tvStrServiceName.setText("");
}
});
完事
AutoCompleteTextView文章:https://github.com/relish-wang/AutoCompleteTextViewSample