实现谷歌播放和搜索栏android等地图

时间:2015-05-29 05:15:38

标签: android android-actionbar material-design android-toolbar android-search

我要求在我的项目中创建Google游戏并使用带过滤器的搜索栏进行映射,但我不知道如何开始。我通过自己的自定义实现尝试了它,如下所示:

在ToolBar android

中自动完成TextView

MainActivity.java

toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar (toolbar);

        ArrayList<String> searchArrayList = new ArrayList<String>();

        searchArrayList.add("United States");
        searchArrayList.add("France");
        searchArrayList.add("United Kingdom");
        searchArrayList.add("India");
        searchArrayList.add("Indonesia");
        searchArrayList.add("Germany");
        searchArrayList.add("Spain");

        AutoCompleteAdapter adapter = new AutoCompleteAdapter(this,
                R.layout.list_item, R.id.textView1, searchArrayList);

        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.searchAutoComplete);
        autoCompleteTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
        autoCompleteTextView.setAdapter(adapter);

使用ArrayAdapter自定义AutoCompleteAdapter类实现

AutoCompleteAdapter.java

public class AutoCompleteAdapter extends ArrayAdapter<String> implements
        Filterable {

    private ArrayList<String> fullList;
    private ArrayList<String> mOriginalValues;
    private ArrayFilter mFilter;

    public AutoCompleteAdapter(Context context, int resource,
                               int textViewResourceId, ArrayList<String> fullList) {

        super(context, resource, textViewResourceId, fullList);
        this.fullList = fullList;
        mOriginalValues = new ArrayList<String>(fullList);

    }

    @Override
    public int getCount() {
        return fullList.size();
    }

    @Override
    public String getItem(int position) {
        return fullList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {
        private Object lock;

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (lock) {
                    mOriginalValues = new ArrayList<String>(fullList);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized (lock) {
                    ArrayList<String> list = new ArrayList<String>(
                            mOriginalValues);
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                final String prefixString = prefix.toString().toLowerCase();

                ArrayList<String> values = mOriginalValues;
                int count = values.size();

                ArrayList<String> newValues = new ArrayList<String>(count);

                for (int i = 0; i < count; i++) {
                    String item = values.get(i);
                    if (item.toLowerCase().contains(prefixString)) {
                        newValues.add(item);
                    }

                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {

            if (results.values != null) {
                fullList = (ArrayList<String>) results.values;
            } else {
                fullList = new ArrayList<String>();
            }
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

我知道上面的实现不是正确的方法。但我需要知道是否有任何其他方式或库来实现它。我也看到,最近几天许多应用程序都使用类似的搜索栏,请帮助我解决方案,因为没有我可以看到这些要求的确切解决方案。

在大多数博客和网站中,只有解决方案like

很抱歉这样说,大多数StackOverflow帖子也没有回答类似的问题如下:

Implement search bar like in play store app

How to create search bar look like google play

欢迎您提供所有解决方案和建议。如果我的帖子不清楚并且似乎无关紧要,请原谅我。期待你的答案。提前致谢

0 个答案:

没有答案