首页 > 解决方案 > 带有回收站视图的搜索视图在片段中不起作用

问题描述

我在 Fragment 中使用 RecyclerView 实现了 searchview。问题是,当我按下搜索并键入产品时,没有结果。在尝试了几乎所有发现之后,我处理这个问题超过 2 周。这是我的搜索片段代码:

class SearchFragment : Fragment(R.layout.fragment_search) {
    val s = ArrayList<NewShoe>()
    val displays=ArrayList<NewShoe>()
    lateinit var adapter:AdapterSearch
    var sp=ArrayList<Spec>()
    override fun onCreate(savedInstanceState: Bundle?) {
        setHasOptionsMenu(true)
        super.onCreate(savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {




        val recyclerView= view?.findViewById<RecyclerView>(R.id.recyclerViewSearch)
        recyclerView.layoutManager= GridLayoutManager(this.context?.applicationContext,2)
        recyclerView.isNestedScrollingEnabled=false
        recyclerView.setHasFixedSize(true)
        searchView.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                println("here1")
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
               adapter.filter.filter(newText)
                println("here2")
                println(adapter.filter.filter(newText).toString())
                return true
            }
        })
        addList()
        super.onViewCreated(view, savedInstanceState)
    }

    fun addList()
    {
        sp.add(Spec(R.drawable.shield,"Anti-pollution, anti-dust"))
        sp.add(Spec(R.drawable.crossing,"Reusable"))
        sp.add(Spec(R.drawable.happy_face,"Pleated at sides for extra comfort"))
        sp.add(Spec(R.drawable.sun,"Wider face coverage for maximum \n" + "protection"))
        s.add(
            NewShoe(
                R.drawable.air_max_london, "Nike air max 1", "LONDON",289,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.air_max_1_have_a_nikeday, "Nike air max 1", "Have A Nike Day",149,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.limeade, "Nike air max 1", "Limeade",209,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.jordan_canyon, "Jordan 1 mid", "\" Canyon Rust\"", 230,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.jordan_particle, "Jordan 1 mid", "SE Particle Beige", 210,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.jordan_yellow, "Jordan 1 mid", "SE Voltage Yellow", 170,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.airmax_90_crock, "Nike airmax 90", "Croc", 190,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.dance_floor_green, "Nike airmax 90", "90S Dancefloor Green", 190,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        s.add(
            NewShoe(
                R.drawable.duck_camo, "Nike airmax 90", "Duck Camo Orange", 140,"",
                arrayListOf(39,40,41), arrayListOf(
                    Spec(0,"Anti-pollution, anti-dust"),
                    Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
                            "protection ")
                ))
        )
        displays.addAll(s)
        val recyclerView= view?.findViewById<RecyclerView>(R.id.recyclerViewSearch)
        recyclerView?.adapter= AdapterSearch(s,object: AdapterSearch.OnClickListener{
            override fun onItemClick(position: Int) {
                val i= Intent(activity,BuyingProducts::class.java)
                i.putExtra("price",s[position].price)
                i.putExtra("name",s[position].name)
                i.putExtra("model",s[position].model)
                i.putExtra("sizes",s[position].sizes)
                i.putExtra("picture",s[position].image)
                i.putExtra("spec",sp)
                startActivity(i)
            }
        })

    }
}

在适配器中,我实现了 Filter 方法,创建了一个新的数组列表。适配器的代码:

class AdapterSearch(private var li:ArrayList<NewShoe>, val onClickListener:OnClickListener):
    RecyclerView.Adapter<RecyclerView.ViewHolder>(),Filterable
{
     var search=ArrayList<NewShoe>()
    lateinit var mcontext: Context
    interface OnClickListener
    {
        fun onItemClick(position:Int)
    }
    class ShoeHolder(itemView: View): RecyclerView.ViewHolder(itemView)
    init {
        search=li
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val searchView= LayoutInflater.from(parent.context.applicationContext).inflate(R.layout.card_jordan,parent,false)
        val sch = ShoeHolder(searchView)
        mcontext = parent.context
        return sch
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.apply {
            search[position].image?.let {
                itemView.findViewById<ImageView>(R.id.jordan_image).setImageResource(it)
            }
            itemView.findViewById<TextView>(R.id.jordan_name).text=search[position].name
            itemView.findViewById<TextView>(R.id.jordan_model).text=search[position].model
            itemView.findViewById<TextView>(R.id.price_jordan).text=search[position].price.toString()
        }
        holder.itemView.setOnClickListener {
            onClickListener.onItemClick(position)
        }
    }
    override fun getItemCount(): Int {
        return search.size
    }
    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(constraint: CharSequence?): FilterResults {
                val queryString = constraint.toString()

                if(queryString.isEmpty())
                    search=li
                 else {
                     val resultList=ArrayList<NewShoe>()
                    for(it in li)
                    {
                        if(it.name.toLowerCase(Locale.ROOT)
                            .contains(queryString.toLowerCase()) || it.price.toString()
                            .contains(queryString.toLowerCase()) ||
                                it.model.toLowerCase(Locale.ROOT)
                                    .contains(queryString.toLowerCase()))
                                        resultList.add(it)
                    }
                    search=resultList
                }
                val filterResults = FilterResults()
                filterResults.values = search
                return filterResults
            }
            override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
                search = filterResults.values as ArrayList<NewShoe>
                notifyDataSetChanged()
            }
        }
    }
}

标签: androidkotlinandroid-recyclerviewadaptersearchview

解决方案


适配器中的 notifyDataSetChanged() 可能不起作用。查看这篇文章了解更多信息: Android: notifyDataSetChanged(); 不工作


推荐阅读