create filter with com.google.android.material.search.SearchView

z31licg0  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(118)

我在Material Components Android中使用了以下代码:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent``your text``">

  <!-- NestedScrollingChild goes here (NestedScrollView, RecyclerView, etc.). -->
  <androidx.core.widget.NestedScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/searchbar_scrolling_view_behavior">
    <!-- Screen content goes here. -->
  </androidx.core.widget.NestedScrollView>

  <com.google.android.material.appbar.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <com.google.android.material.search.SearchBar
        android:id="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/searchbar_hint" />
  </com.google.android.material.appbar.AppBarLayout>

  <com.google.android.material.search.SearchView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:hint="@string/searchbar_hint"
      app:layout_anchor="@id/search_bar">
    <!-- Search suggestions/results go here (ScrollView, RecyclerView, etc.). -->
  </com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

但我不知道如何过滤与此searchview,我只看到说明androidx.appcompat.widget.SearchView如果我替换为androidx.appcompat.widget.SearchView,当我点击搜索栏,searchview不出现

pnwntuvh

pnwntuvh1#

只需在Activity中添加此代码即可显示SearchView并处理backpress

with(binding) {
        searchView
            .editText
            .setOnEditorActionListener { v, actionId, event ->
                searchBar.text = searchView.text
                searchView.hide()
                false
            }
    }

或者,您也可以删除CoordinatorLayout,只需要手动连接即可

searchView.setupWithSearchBar(searchBar)

相关问题