SearchView中的下拉列表

时间:2016-08-29 12:28:04

标签: android toolbar searchview

我在屏幕上创建活动。我在工具栏中阅读了很多关于SearchView的教程,并在我的活动中实现了它并且它可以工作(上面的代码)。但是如何制作这个下拉列表呢?感谢您的想法,链接或代码。

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.second_activity_menu, menu);


        MenuItem searchItem = menu.findItem(R.id.second_toolbar_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
       // ...
   }

Activity I need to create

1 个答案:

答案 0 :(得分:1)

在您的gradle中添加此依赖项:

dependencies {
    compile 'com.miguelcatalan:materialsearchview:1.0.0'
}

将此添加到您的主要活动:

<RelativeLayout
    android:id="@+id/toolbar_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/theme_primary" />

    <com.miguelcatalan.materialsearchview.MaterialSearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

创建一个菜单文件:

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_action_search"
    android:orderInCategory="100"
    android:title="@string/abc_search_hint"
    app:showAsAction="always" />

将此添加到您的MainActivity:

MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //Do some magic
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            //Do some magic
            return false;
        }
    });

    searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {
            //Do some magic
        }

        @Override
        public void onSearchViewClosed() {
            //Do some magic
        }
    });

这是OnActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MaterialSearchView.REQUEST_VOICE && resultCode == RESULT_OK) {
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        if (matches != null && matches.size() > 0) {
            String searchWrd = matches.get(0);
            if (!TextUtils.isEmpty(searchWrd)) {
                searchView.setQuery(searchWrd, false);
            }
        }

        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

在字符串res中创建一些数组列表:

<string-array name="query_suggestions">
    <item>Android</item>
    <item>iOS</item>
    <item>SCALA</item>
    <item>Ruby</item>
    <item>JavaScript</item>
</string-array>

将字符串res添加到搜索文本框:

searchView.setSuggestions(getResources().getStringArray(R.array.query_suggestions));