android - Kotlin 导航 fragment 中的 searchview.setonquerytextlistener() 类型不匹配

标签 android kotlin searchview

我收到“需要 OnQueryTextListener 的类型不匹配!”尝试在导航 fragment 中的 Kotlin 中实现 searchview 时出错。我搜索了尽可能多的示例和 stackoverflow 问题,一切都表明我的代码应该是正确的。请注意,我的 searchview 是 PERSISTANT(不是菜单的一部分),所以我不能做 menu.finditem。

enter image description here

这是我的 HomeFragment 代码:

class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_home, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    println("***************** Home Fragment *******************")

    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String): Boolean {
            // task HERE
            return false
        }
        override fun onQueryTextChange(newText: String): Boolean {
            return false
        }
    })
}

和布局代码:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/materialBackgroundGrey">

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:iconifiedByDefault="false"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:queryBackground="@null">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search"
        app:iconifiedByDefault="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.cardview.widget.CardView>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/colorWhite"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/cardView"
    app:layout_constraintBottom_toBottomOf="parent"
    />

最佳答案

有两个SearchView小部件:

  • android.widget.SearchView
  • androidx.appcompat.widget.SearchView

  • 它们扮演相同的角色但不兼容,它们的嵌套接口(interface),如 OnQueryTextListener , 不兼容。

    确保在资源(例如布局)和任何 import 中都使用相同的资源。陈述。如果您使用 AppCompat,您可能需要 androidx.appcompat.widget.SearchView .

    关于android - Kotlin 导航 fragment 中的 searchview.setonquerytextlistener() 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61034420/

    相关文章:

    android - OkHttp3类未找到异常,但存在于apk的dex文件中

    android - titleList 显示不正确..?

    android - 如何禁用 Kotlin 中的某些 PopupMenu 项?

    java - 调用 invalidateOptionsMenu() 会导致 SearchView 停止工作

    android - SearchView 未向结果 Activity 发送 Intent

    android - SearchView 通过 EditText android

    android r.java 不会更新

    java - 在 Android 中存储数据的最佳方式

    android - 如何在android TextView中使用Get Ordered列表

    android - 在 Intent 中覆盖 OnActivityResult 不起作用 - kotlin