简体   繁体   English

当第一项更改时,RecyclerView 滚动到更改的项目位置

[英]RecyclerView scrolls to changed item location, when first item changes

I have a RecyclerView with a checkbox for every item.我有一个RecyclerView ,每个项目都有一个复选框。 When checkbox pressed it is moved to the bottom of the list.当复选框被按下时,它被移动到列表的底部。 When i check any item besides the first one everything is OK but if i check the first one, it auto-scroll to the new item location.当我检查除第一个项目之外的任何项目时,一切正常,但如果我检查第一个项目,它会自动滚动到新项目位置。

I redesigned the adapter using a recent codelab but still the same issue我使用最近的 codelab 重新设计了适配器,但仍然是同样的问题

class ItemsAdapter(val clickListener: ItemAdapterListener):
    ListAdapter<UiQueryItem<Item>, ItemsAdapter.ViewHolder>(asyncDifferConfig) {

    companion object {
        private val diffCallback = object : UiQueryItemDiffCallback<Item>() {}
        private val executors = AppExecutors()
        private val asyncDifferConfig =
            AsyncDifferConfig.Builder<UiQueryItem<Item>>(diffCallback)
                .setBackgroundThreadExecutor(executors.cpuExecutorService)
                .build()
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        holder.bind(item,clickListener)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder.from(parent)
    }

    class ViewHolder private constructor(val binding: ListItemBinding): RecyclerView.ViewHolder(binding.root){

        fun bind(item: UiQueryItem<Item>, clickListener: ItemAdapterListener) {
            binding.item = item.item
            binding.clickListener = clickListener
            binding.isSelected = item.isFlag1
            binding.executePendingBindings()
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val binding = ListItemBinding.inflate(layoutInflater, parent, false)
                return ViewHolder(binding)
            }
        }
    }
}

I think i found a workaround.我想我找到了解决方法。

I noticed the next github issue solution:我注意到下一个 github 问题解决方案:

Automatic scrolling when new data set #224 新数据集时自动滚动 #224

It did not work in my case but after tweaking a bit i found an answer.它在我的情况下不起作用,但经过一些调整后,我找到了答案。 Please note that it is only a workaround and might make some bugs if you are trying to implement your own auto-scroll solution.请注意,这只是一种解决方法,如果您尝试实现自己的自动滚动解决方案,可能会产生一些错误。 The Workaround simply checkes if an item changed position from first place and if so, gets back to first position解决方法只是检查项目是否从第一个位置更改了 position,如果是,则返回第一个 position

Also note that @elihart is suggesting other solutions (check the link)另请注意,@elihart 建议其他解决方案(查看链接)

private val dataObserver = object : RecyclerView.AdapterDataObserver() {
        override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
            super.onItemRangeMoved(fromPosition, toPosition, itemCount)
            if (fromPosition == 0) {
                binding.itemRv.scrollToPosition(0)
            }
        }
    }

override fun onResume() {
    super.onResume()
    adapter.registerAdapterDataObserver(dataObserver)
}

override fun onPause() {
    super.onPause()
    adapter.unregisterAdapterDataObserver(dataObserver)
}

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

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