实施Android SearchView建议,如Google Play商店

时间:2017-11-17 21:03:08

标签: android

如何在Google Play商店等许多应用中使用覆盖Google素材SearchView等其他视图的SearchView建议视图? enter image description here

1 个答案:

答案 0 :(得分:0)

public class AutoSuggestAdapter extends ArrayAdapter {

private Context context;
private int resource;
private List<String> items;
private List<String> tempItems;
private List<String> suggestions;

public AutoSuggestAdapter(Context context, int resource, List<String> items) {
    super(context, resource, 0, items);

    this.context = context;
    this.resource = resource;
    this.items = items;
    tempItems = new ArrayList<String>(items);
    suggestions = new ArrayList<String>();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, parent, false);
    }

    String item = items.get(position);

    if (item != null && view instanceof TextView) {
        ((TextView) view).setText(item);
    }

    return view;
}

@Override
public Filter getFilter() {
    return nameFilter;
}

Filter nameFilter = new Filter() {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        String str = (String) resultValue;
        return str;
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();
            for (String names : tempItems) {
                if (names.toLowerCase().contains(constraint.toString().toLowerCase())) {
                    suggestions.add(names);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        List<String> filterList = (ArrayList<String>) results.values;
        if (results != null && results.count > 0) {
            clear();
            for (String item : filterList) {
                add(item);
                notifyDataSetChanged();
            }
        }
    }
};

这应该是您自定义的适配器类。 在您的主要部分中,您使用构造函数调用此类,它将如下所示。

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView ACTV;
List<String> suggestions = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ACTV = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
}
public void AutocmpleteMeth() {
    // Hieronder is het code voor Autocomplete [BEGIN]
    AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, suggestions);
    ACTV.setAdapter(adapter);
    ACTV.setThreshold(1);

}}