单击RecyClerview中的项目后,单击“ onItem”单击不正常



我试图在回收器视图中实现搜索功能。因为我在下面的" main.kt"类中写了下面的代码

搜索功能对我来说很好,

问题是当我从列表中搜索该项目并单击该项目时,我会遇到错误的位置。

请帮助我解决这个问题。

adapter = DynamicListAdapter(dynamicList)
        dynamic_list_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL,false) as RecyclerView.LayoutManager
        dynamic_list_recyclerview.adapter = adapter
        adapter!!.setRecyclerViewItemClickLister(this)

我添加了编辑文本并添加了AddTextchangedListener

edt_search_dynamic.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun afterTextChanged(editable: Editable?) {
                filter(editable.toString())
            }
        })

fun filter(text : String){
    var filteredList =  mutableListOf<DynamicModel>()
    for(list in dynamicList){
        if(list.vehicleno.toLowerCase().contains(text.toLowerCase())){
            filteredList.add(list)
        }
    }
    adapter?.filterList(filteredList)
}

这是我的适配器类

class DynamicListAdapter (var dynamiclist : List<DynamicModel>) : RecyclerView.Adapter<DynamicListAdapter.DynamicViewHolder>()  {
    var recyclerViewOnItemClickListener : RecyclerViewOnItemClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DynamicViewHolder {
        val inflatedView = LayoutInflater.from(parent.context)
                .inflate(R.layout.singlerowdynamiclist,parent,false)
        return DynamicViewHolder(inflatedView,dynamiclist)
    }
    override fun getItemCount(): Int {
        return dynamiclist.size
    }
    override fun onBindViewHolder(holder: DynamicViewHolder, position: Int) {
        holder.bindItem(dynamiclist[position])
    }
    inner class DynamicViewHolder(itemView : View, var dynamiclists : List<DynamicModel>) : RecyclerView.ViewHolder(itemView), View.OnClickListener
    {
        private var txtStatus : TextView? = null
        init {
            txtStatus = itemView.findViewById(R.id.txtStatus)
            itemView.setOnClickListener(this)
        }
        fun bindItem(dynamiclist : DynamicModel){
            txtStatus?.text  = dynamiclist.vehiclestatus
        }
        override fun onClick(view: View?) {
            when(view){
                itemView -> if(recyclerViewOnItemClickListener!= null){
                    recyclerViewOnItemClickListener!!.onItemClick(adapterPosition,view)
                }
            }
        }
    }
    // method of outer class
    fun setRecyclerViewItemClickLister(onRecyclerviewItemClickListener : RecyclerViewOnItemClickListener){
        this.recyclerViewOnItemClickListener = onRecyclerviewItemClickListener
    }
    // for filter the list
    fun filterList(dymaniclist : List<DynamicModel>){
        this.dynamiclist = dymaniclist
        notifyDataSetChanged()
    }
}

这是我在Main.kt类中实现的界面

interface RecyclerViewOnItemClickListener {
    fun onItemClick(position : Int, view: View)
}

应用过滤器后,您将无法获得所需的位置(未过滤列表的位置(,因为在创建新的过滤列表并将其放置在DynamicListAdapter中时,您会丢失该信息。

adapter中说,我们有以下项目的列表:

val dynamicList = listOf(
    "#0 A", // 0th position
    "#1 B", // 1st position
    "#2 AB" // 2nd position
)

现在让我们使用您的filter方法:

filter("A")

以全新的filteredList替换DynamicListAdapter内部的旧dynamicList结束,看起来像这样:

val filteredList = listOf(
    "#0 A",  // 0th position
    "#2 AB", // 1st position
)

现在,当单击#2 AB时,您将获得值1的位置(不需要的2(,因为它是新的filteredList中的项目索引。


有多种方法如何实现所需的目标,但是,如果您只需要单击项目中的数据,只需返回对象而不是其在项目上的位置:

interface RecyclerViewOnItemClickListener {
    fun onItemClick(dynamicModel: DynamicModel, view: View)
}

// inside DynamicListAdapter
inner class DynamicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bindItem(dynamiclist : DynamicModel) {
        with (itemView) {
            txtStatus.text  = dynamiclist.vehiclestatus
            setOnClickListener { 
                recyclerViewOnItemClickListener?.onItemClick(dynamiclist, this)
            }
        }
    }        
}

但是,如果您需要返回该位置,并且不是一个选项,我建议将原始列表留在adapter中,然后让其单独处理过滤,例如:

// _dynamicList is always the original list
class DynamicListAdapter (private val _dynamicList: List<DynamicModel>) : RecyclerView.Adapter<DynamicListAdapter.DynamicViewHolder>()  {
    var recyclerViewOnItemClickListener : RecyclerViewOnItemClickListener? = null
    var filterText: String = ""
        set(value) {
            field = value
            notifyDataSetChanged()
        }
    // return original list (_dynamicList) if no filter should be applied or return the filtered list based on the filterText
    private val dynamicList: List<DynamicModel>
        get() = if (filterText.isBlank()) _dynamicList
                else _dynamicList.filter { it.vehicleno.contains(filterText, ignoreCase = true) }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DynamicViewHolder {
        val inflatedView = LayoutInflater.from(parent.context).inflate(R.layout.singlerowdynamiclist,parent,false)
        return DynamicViewHolder(inflatedView)
    }
    override fun getItemCount(): Int {
        return dynamicList.size
    }
    override fun onBindViewHolder(holder: DynamicViewHolder, position: Int) {
        holder.bindItem(dynamicList[position])
    }
    inner class DynamicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindItem(dynamicModel: DynamicModel) {
            with (itemView) {
                txtStatus.text = dynamicModel.vehiclestatus
                // get the item's original position (before filtering)
                val position = _dynamicList.indexOf(dynamicModel)
                setOnClickListener {
                    recyclerViewOnItemClickListener?.onItemClick(position, this)
                }
            }
        }
    }
}
// filter method
fun filter(text: String){
    adapter?.filterText = text
}

我提出了解决方案,它可能会帮助某人。

给定:recyclerview工作正常。

问题:实现搜索视图后,单击在项目上不起作用。

解决方案:在onQueryTextChange中再次添加mAdapter.setOnItemClickListener如下:

@Override
            public boolean onQueryTextChange(String newText) {
                
                mAdapter = new MyAdapter(getApplicationContext(), OtherInofYouWantToPass);
                recyclerView.setAdapter(mAdapter);
                mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, MyObj obj, int position) {

                        { add your work here}
                    }

                });                    
                return false;
            }

相关内容

  • 没有找到相关文章

最新更新