gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:31:40 24 4
gpt4 key购买 nike

我收到“需要 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com