向上滚动recyclerview android时项目正在更新?

Posted

技术标签:

【中文标题】向上滚动recyclerview android时项目正在更新?【英文标题】:item are updating while scrolling up reyclerview android? 【发布时间】:2021-10-29 02:34:37 【问题描述】:

我正在制作一个应用程序,我必须在其中将数据插入到 reyclerview 中,recyclerview 工作正常,但问题是当我向上滚动它时适配器会重新更新数据,所以要解决这个问题,代码看起来很好但仍然得到这个问题 ................................................ ....

基类

abstract class MultiViewModelBaseAdapter<M : Model, VDB : ViewDataBinding>(private var diffCallback: DiffUtil.ItemCallback<M>) : ListAdapter<M ,BaseViewHolder<VDB>>(diffCallback) 

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VDB> 
        val inflator = LayoutInflater.from(parent.context)
        val binding = createBinding(viewType, inflator, parent)

        return BaseViewHolder(binding) 
    

    abstract fun createBinding(viewType: Int, inflater: LayoutInflater, parent: ViewGroup) : VDB

    override fun onBindViewHolder(holder: BaseViewHolder<VDB>, position: Int) 
        bind(holder.mBinding, getItem(position), position)
        holder.mBinding.executePendingBindings()
    

    abstract fun bind(binding: VDB, item: M, position: Int)


    abstract fun onDataChanged(values: Boolean)

适配器

 class LanguageAdapter(
    private val context: Context,
    private val mViewModel: LanguageListViewModel,
    private val onClickListener: OnItemClickListener<String>
) : MultiViewModelBaseAdapter<LanguageSupportModel, ViewDataBinding>(diffCallback) 
    companion object 
        private val ADS = 1
        private val LANGUAGES = 2

        val diffCallback = object : DiffUtil.ItemCallback<LanguageSupportModel>() 
            override fun areItemsTheSame(
                oldItem: LanguageSupportModel,
                newItem: LanguageSupportModel
            ): Boolean = oldItem.dataId == newItem.dataId

            /**
             * Note that in kotlin, == checking on data classes compares all contents, but in Java,
             * typically you'll implement Object#equals, and use it to compare object contents.
             */
            override fun areContentsTheSame(
                oldItem: LanguageSupportModel,
                newItem: LanguageSupportModel
            ): Boolean = oldItem == newItem
        
    

    override fun getItemId(position: Int): Long 
        return position.toLong()
    

    override fun getItemViewType(position: Int): Int 
        return position
    

    override fun createBinding(viewType: Int, inflater: LayoutInflater, parent: ViewGroup): ViewDataBinding 
        return DataBindingUtil.inflate(inflater, R.layout.language_view, parent, false)
    

    override fun bind(binding: ViewDataBinding, item: LanguageSupportModel, position: Int) 
        binding as LanguageViewDataBinding
        binding.apply 
            language = item
            //click
        
    

    override fun onDataChanged(values: Boolean) 

片段

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

    val languageAdapter = LanguageAdapter(requireContext(), mViewModel, this ,lifecycleScope)
    languageAdapter.submitList(LanguageArray.arrayValues())
    reyclerview.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    adapter = languageAdapter

【问题讨论】:

您的问题不清楚。您是在告诉您例如在 recyclerView 的项目中编辑一些 EditText,并且在滚动然后回滚时编辑会丢失? i have to insert data into reyclerview 好的。但你没有说你是否确实这样做了。什么时候。而且你也没有说明你将如何做到这一点......或者你是如何做到的...... @blackapps 问题是适配器在滚动时刷新数据 您的问题仍然不清楚,因为您没有详细说明。 【参考方案1】:

如果您将在滚动中使用分页概念,那么它会解决。

在JAVA或者KOTLIN中我们可以实现..like..

导入androidx.viewpager.widget.PagerAdapter;

然后将其扩展为适配器类。

public void addList(List<ClsList> list) 
    this.mResources = list;
    notifyDataSetChanged(); // also this main line

问题解决。 ☻♥玩得开心..

【讨论】:

以上是关于向上滚动recyclerview android时项目正在更新?的主要内容,如果未能解决你的问题,请参考以下文章

如何在向上滚动时清除 RecyclerView 中的列表

插入数据时Recyclerview自动向上滚动

Android - 在 CoordinatorLayout 中使用时页脚滚动到屏幕外

键盘打开时相应向上滚动 RecyclerView

向下滚动然后再次向上滚动后,recyclerview 项目之间出现间隙。请看详情

再次向上滚动后,带有 Glide 的 RecyclerView 导致 NullPointer