为啥 recyclerview 的 searchview 不起作用?

Posted

技术标签:

【中文标题】为啥 recyclerview 的 searchview 不起作用?【英文标题】:Why searchview for recyclerview is not working?为什么 recyclerview 的 searchview 不起作用? 【发布时间】:2021-09-13 20:58:16 【问题描述】:

我做了我的适配器并制作了一个可以过滤我的列表的函数。但问题是它没有显示结果,我不知道为什么。有 2 个列表:其中一个是完整列表,另一个是过滤列表。这是我的适配器:

class SearchAdapter : RecyclerView.Adapter<SearchViewHolder>(), Filterable 

    var fullInstitutionData: ArrayList<InstitutionModel> = ArrayList()
    var institutionData: ArrayList<InstitutionModel> = ArrayList()

    init 
        institutionData = ArrayList()
    

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder 
        val inflater = LayoutInflater.from(parent.context)
        return SearchViewHolder(inflater, parent)
    

    override fun getItemCount(): Int = institutionData.size

    override fun onBindViewHolder(holder: SearchViewHolder, position: Int) 
        val dialogData: InstitutionModel = institutionData[position]
        holder.bind(dialogData)
    

    override fun getFilter(): Filter 
        return customFilter
    

    fun addDataFromClient(listDataClient: ArrayList<InstitutionModel>) 
        institutionData.addAll(listDataClient)
        fullInstitutionData = ArrayList(listDataClient)
        notifyDataSetChanged()
    

    private val customFilter = object : Filter() 
        override fun performFiltering(constraint: CharSequence?): FilterResults 
            val filteredList = arrayListOf<InstitutionModel>()
            if (constraint == null || constraint.length == 0) 
                filteredList.addAll(institutionData)
             else 
                val filterPattern = constraint.toString().toLowerCase().trim()
                for (item in fullInstitutionData) 
                    if (item.name.toLowerCase().contains(filterPattern) || item.name?.toLowerCase()!!
                                    .contains(filterPattern)) 
                        filteredList.add(item)
                    
                
            
            val results = FilterResults()
            results.values = filteredList
            return results
        

        override fun publishResults(constraint: CharSequence?, filterResults: FilterResults?) 
            institutionData.clear()
            institutionData.addAll(filterResults?.values as MutableList<InstitutionModel>)

        

    


这是我在片段中使用setOnQueryListener 的搜索功能:

 fun searchInstitution() 
        searchViewInstitutions.apply 
            setOnQueryTextListener(object : SearchView.OnQueryTextListener 
                override fun onQueryTextSubmit(query: String): Boolean 
                    return false
                

                override fun onQueryTextChange(newText: String?): Boolean 
                    searchAdapter.filter.filter(newText)
                    return false
                

            )

        
    

这是我在 onActivityCreated 上调用 recyclerview 时的代码:

 val llm = LinearLayoutManager(context)
    llm.orientation = LinearLayoutManager.VERTICAL
    rvSearch.layoutManager = llm
    rvSearch.adapter = SearchAdapter()

    searchAdapter.addDataFromClient(data)
    searchInstitution()

【问题讨论】:

【参考方案1】:

需要两个修复:

notifyDataSetChanged() 当你publishResults()

 override fun publishResults(constraint: CharSequence?, filterResults: FilterResults?) 
     institutionData.clear()
     institutionData.addAll(filterResults?.values as MutableList<InstitutionModel>)
     notifyDataSetChanged() // <<< Change Here
 

当没有匹配时添加完整列表而不是过滤后的列表

 override fun performFiltering(constraint: CharSequence?): FilterResults 
     val filteredList = arrayListOf<InstitutionModel>()
     if (constraint == null || constraint.length == 0) 
         filteredList.addAll(fullInstitutionData) // <<< Change Here

【讨论】:

感谢您的回答,但仍然无法正常工作。我在描述中添加了模拟器的屏幕截图。 @Gabriel 你能在publishResults() 中使用institutionData = filterResults?.values as MutableList&lt;InstitutionModel&gt; 代替clear()addAll() 我收到此错误:Type mismatch: inferred type is MutableList&lt;InstitutionModel&gt; but kotlin.collections.ArrayList&lt;InstitutionModel&gt; /* = java.util.ArrayList&lt;InstitutionModel&gt; */ was expected。我更改为 ArrayList 但仍然无法正常工作。 试试这个institutionData = filterResults?.values as ArrayList&lt;InstitutionModel&gt; 我这样做并运行了项目,但没有改变

以上是关于为啥 recyclerview 的 searchview 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 recyclerview 返回错误的项目?

如图,Android 编程,右侧三个recyclerview布局,为啥是靠顶部,不是居中的?布局

为啥 CardView 和 RecyclerView 需要 minSdkVersion L?

为啥我的 RecyclerView 没有填充来自改造的数据?

为啥我的 RecyclerView 不显示任何内容?

为啥 findViewById 搜索 RecyclerView 时返回 null?