返回

ViewPager2中的android-ListAdapter在每次调用submit list时都会调用onBindViewHolder两次

发布时间:2022-04-20 12:34:43 349
# 数据

正如标题所说。在使用多视图持有者的ViewPager2上,ListAdapter在单击一个按钮后两次调用BindViewHolder,该按钮会强制刷新数据,从而更改模特儿。Isstylusinputable不管是真是假。以下是一个名为FirstViewHolder注意Isstylusinputable对于这两个视图持有者是不同的。这会导致一个问题,因为它会将我的UI重置为false,即使我将其设置为true

2022-04-19 08:23:07.437 8667-8667/com。样品dev D/TEST:加载BindViewHolder模型。isStylusInputEnabled true此FirstViewHolder{e930e86 position=0 id=-1,oldPos=-1,pLpos:-1不可回收(1)无父}

2022-04-19 08:23:07.496 8667-8667/com。样品dev D/TEST:加载BindViewHolder模型。isStylusInputEnabled false此FirstViewHolder{843d958 position=0 id=-1,oldPos=-1,pLpos:-1废料[ChangeScrapt]tmpDetached不可回收(3)无父项}

ListAdapter代码

class MultipleViewAdapter(
val firstFlow: Flow>,
val secondFlow: Flow>,
val thirdFlow: Flow>) : ListAdapter(DefaultDiffUtil()) {

companion object {
    const val VIEW1 = 0
    const val VIEW2 = 1
    const val VIEW3 = 2
}

private val scope = CoroutineScope(Dispatchers.Main)

var onRemarksChangeListener: OnRemarksChangeListener? = null

fun interface OnRemarksChangeListener {
    fun onChange(position: Int, value: String)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    return when(viewType) {
        VIEW1 -> FirstViewHolder(ItemFirstBinding.inflate(LayoutInflater.from(parent.context),  parent, false))
        VIEW2 -> SecondConvectionViewHolder(ItemSecondBinding.inflate(LayoutInflater.from(parent.context),  parent, false))
        else -> ThirdViewHolder(ItemThirdBinding.inflate(LayoutInflater.from(parent.context),  parent, false))
    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    holder.setIsRecyclable(false)
    when(holder) {
        is FirstViewHolder -> holder.bind(position)
        is SecondViewHolder -> holder.bind(position)
        is ThirdViewHolder -> holder.bind(position)
    }
}


//necessary for onBindViewHolder to render position correctly
override fun getItemViewType(position: Int): Int {
    return getItem(position).pageType
}

//necessary for onBindViewHolder to render position correctly
override fun getItemId(position: Int): Long {
    return position.toLong()
}

fun onDestroy() {
    scope.cancel()
}


inner class FirstViewHolder(private val _binding: ItemFirstBinding) : RecyclerView.ViewHolder(_binding.root), StylusInput {

    private val _adapter by lazy {
        FirstAdapter()
    }

    override val pathCollection: List>>
        get() = _binding.canvas.pathCollection

    override var isEnabled: Boolean = false
        set(value) {
            with(_binding) {
                container.isScrollable = !value
                canvas.isWritable = value
            }

            field = value
        }

    fun bind(position: Int) {
        val model = getItem(position)

       
        Log.d("TEST", "load onBindViewHolder ${model} model.isStylusInputEnabled ${model.isStylusInputEnabled} this ${this}}")

        isEnabled = model.isStylusInputEnabled
        _binding.canvas.loadPathCollection(model.stylusInput)


        _binding.rvFirst.adapter = _adapter
        scope.launch {
            firstFlow.collectLatest {
                _adapter.submitList(it) {}
            }

            _binding.rvFirst.viewTreeObserver.addOnDrawListener {
                _binding.canvas.refresh(model.stylusInput)
            }
        }
    }
}

inner class SecondViewHolder(private val _binding: ItemSecondBinding) : RecyclerView.ViewHolder(_binding.root), StylusInput {

    private val _adapter by lazy {
        SecondAdapter()
    }

    override val pathCollection: List>>
        get() = _binding.canvas.pathCollection


    override var isEnabled: Boolean = false
        set(value) {
            with(_binding) {
                container.isScrollable = !value
                canvas.isWritable = value
            }

            field = value
        }

    fun bind(position: Int) {
        val model = getItem(position)
      
        isEnabled = model.isStylusInputEnabled
        _binding.canvas.loadPathCollection(model.stylusInput)


        _binding.rvSecond.adapter = _adapter
        scope.launch {
            secondFlow.collectLatest {
                _adapter.submitList(it)
            }

            _binding.rvSecond.viewTreeObserver.addOnDrawListener {
                _binding.canvas.refresh(model.stylusInput)
            }
        }
    }
}

inner class ThirdViewHolder(private val _binding: ItemThirdBinding) : RecyclerView.ViewHolder(_binding.root), StylusInput {

    private val _adapter by lazy {
        ThirdAdapter()
    }

    override val pathCollection: List>>
        get() = _binding.canvas.pathCollection

    override var isEnabled: Boolean = false
        set(value) {
            with(_binding) {
                container.isScrollable = !value
                canvas.isWritable = value
            }

            field = value
        }

    fun bind(position: Int) {
        val model = getItem(position)
 
        isEnabled = model.isStylusInputEnabled
        _binding.canvas.loadPathCollection(model.stylusInput)

        _binding.rvThird.adapter = _adapter
        scope.launch {
            thirdFlow.collectLatest {
                _adapter.submitList(it)
            }

            _binding.rvThird.viewTreeObserver.addOnDrawListener {
                _binding.canvas.refresh(model.stylusInput)
            }
        }
    }
}

}

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
下一篇
Typescript从只读数组创建对象类型 2022-04-20 11:34:37