简体   繁体   English

单击项目 Android 时 RecyclerView 滚动

[英]RecyclerView scrolls when click on item Android

I have implemented RecyclerView with changing backround when clicked on an item.单击某个项目时,我已经实现了 RecyclerView 并更改了背景。 RecyclerView contains only TextView. RecyclerView 仅包含 TextView。 Sometimes when I click on an item the recyclerview scroll little bit.有时当我点击一个项目时,recyclerview 会滚动一点。 Strange is, that recyclerview behave like this, sometimes is everything OK.奇怪的是,recyclerview 的行为是这样的,有时一切正常。 I am not sure why the RV is acting like this.我不确定为什么 RV 会这样。

Direct link to gif - Action on RecyclerView直接链接到 gif - RecyclerView 上的操作

This is my Adapter这是我的适配器

    private var selectedItemPosition: Int = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PolozkaViewHolder {
        val binding = PolozkyItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PolozkaViewHolder(binding)
    }
    override fun onBindViewHolder(holder: PolozkaViewHolder, position: Int) {
        val currentItem = getItem(position)
        holder.bind(currentItem)
      
        if (selectedItemPosition == position){
            holder.itemView.setBackgroundColor(Color.parseColor("#DA745A"))
        } else
        {
            holder.itemView.setBackgroundColor(Color.TRANSPARENT)
        }
    }
    inner class PolozkaViewHolder(private val binding: PolozkyItemBinding): RecyclerView.ViewHolder(binding.root){
        init {
            binding.root.setOnClickListener{
                val position = bindingAdapterPosition
                if (position != RecyclerView.NO_POSITION){
                    val item = getItem(position)
                    if (item != null){
                        listener.onItemClick(item, position)
                        //listener.onItemClickListener(item, position)
                    }
                }
                notifyItemChanged(selectedItemPosition)
                selectedItemPosition = absoluteAdapterPosition
                notifyItemChanged(selectedItemPosition)
            }
            binding.root.setOnLongClickListener{
                val positionLong = bindingAdapterPosition
                if (positionLong != RecyclerView.NO_POSITION){
                    val itemLong = getItem(positionLong)
                    if(itemLong != null){
                        listener.onLongClick(itemLong)
                    }
                }
                true
            }            
        }
        fun bind(polozkaPolozka: Polozka){
            binding.apply {
                tvStar.text =  if (polozkaPolozka.mnoz_vyd == 0.toFloat()){
                    ""
                }else (if (polozkaPolozka.mnoz_vyd != polozkaPolozka.mnoz_obj){
                                "-"
                            } else if (polozkaPolozka.mnoz_vyd == polozkaPolozka.mnoz_obj){
                                "*"
                            } else{
                                ""
                            })
                tvKDE.text = polozkaPolozka.znacky
                tvREG.text = polozkaPolozka.reg
                tvVB.text = polozkaPolozka.veb.toString()
                tvMN.text = polozkaPolozka.mnoz_obj.toString()
                tvMNV.text = polozkaPolozka.mnoz_vyd.toString()
                tvDATSPOTR.text = polozkaPolozka.datspo
                tvSARZE.text = polozkaPolozka.sarze

            }
        }        
    }
    interface OnItemClickListener{
        fun onItemClick(polozkaDoklad: Polozka, position: Int)
        fun onLongClick(polozkaDoklad: Polozka) 
    }
    class DiffCallback: DiffUtil.ItemCallback<Polozka>(){
        override fun areItemsTheSame(oldItem: Polozka, newItem: Polozka) =
            oldItem.pvp06pk == newItem.pvp06pk
        override fun areContentsTheSame(oldItem: Polozka, newItem: Polozka) =
            oldItem == newItem
    }
}

And this is my simple Activity这是我的简单活动

class PolozkaActivity: AppCompatActivity(), PolozkaAdapter.OnItemClickListener{
      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      val binding = ActivityPolozkaBinding.inflate(layoutInflater)
    }

    override fun onItemClick(polozkaDoklad: Polozka, position: Int) {
        val resultReg = polozkaViewModel.getInfoByREG(polozkaDoklad.reg.toString())
        textViewStatus.text = "$resultReg"
        positionItem = position
    }

    override fun onLongClick(polozkaDoklad: Polozka) {
        val intent = Intent(this, NewActivity::class.java)
        startActivity(intent)
    }
}    

EDIT: Change of url of gif.编辑:更改 gif 的 url。

ViewHolder.absoluteAdapterPosition returns the position of the root view in pixels relative to the RecyclerView. ViewHolder.absoluteAdapterPosition返回根视图的 position,以相对于 RecyclerView 的像素为单位。 It has nothing to do with the index of the associated item in the list.它与列表中关联项的索引无关。

I think the simplest way to fix what you have is to add a var currentIndex: Int property to your ViewHolder, and set that position in onBindViewHolder .我认为解决问题的最简单方法是将var currentIndex: Int属性添加到 ViewHolder 中,然后在 onBindViewHolder 中设置onBindViewHolder Then change然后改变

            notifyItemChanged(selectedItemPosition)
            selectedItemPosition = absoluteAdapterPosition
            notifyItemChanged(selectedItemPosition)

to

            notifyItemChanged(selectedItemPosition)
            selectedItemPosition = currentIndex
            notifyItemChanged(selectedItemPosition)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM