0

I make a recyclerView and update dataset with notifyDataSetChanged and it will call onCreateViewHolder and onBindViewHolder

Is there any way just call onBindViewHolder but don't call onCreateViewHolder after notifyDataSetChanged? Because i want to save some states in viewholder but still want to update view.

I try to use viewHolder.setIsRecyclable(false) but not works.


class SelectionAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var selectionList = mutableListOf<BetSelection>()

    fun setData(selectionList: List<BetSelection>) {
        this.selectionList = selectionList.toMutableList()
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        
            val viewBinding = SelectionItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
            val viewHolder = SelectionViewHolder(viewBinding, parent.context)
            viewHolder.setIsRecyclable(false)
            return viewHolder
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
      when (holder) {
            is SelectionViewHolder -> {
                holder.setSelection(selectionList[position])
            }
        }
    }

    override fun getItemCount() = selectionList.count()
}
class SelectionViewHolder(
    private val viewBinding: SelectionItemBinding,
    private val context: Context
) : RecyclerView.ViewHolder(viewBinding.root) {

    private var isOddsInitComplete = false

    fun setSelection(selection: Selection) {

    //isOddsInitComplete always false because onCreateViewHolder called everytime
    if(isOddsInitComplete) {
        updateOdds()
    } else {
        initOddsView()
        isOddsInitComplete = true
    }
    .
    .
    .
}
GHH
  • 1,713
  • 1
  • 8
  • 21
  • You can use [`notifyItemChanged()` or `notifyItemRangeChanged`](https://stackoverflow.com/questions/33789345/whats-better-notifydatasetchanged-or-notifyitemchanged-in-loop) – Nitish Jan 05 '22 at 04:53
  • @Nitish Thank for comments, but i want to update all holders. – GHH Jan 05 '22 at 04:57
  • i think you want t update all data but you dont want animation wile recyclerView do when we notified data changed – Amit pandey Jan 05 '22 at 04:59
  • What exactly are you trying to save in the ViewHolder? Could you store that data elsewhere (e.g. in a list in the adapter) and look them up by position instead? I don't think the ViewHolder is really intended for storing things other than interchangeable views – Tyler V Jan 05 '22 at 05:12
  • @TylerV I edit my question, i want to save a boolean in viewholder that decides what i need to do – GHH Jan 05 '22 at 05:21
  • So could you just save a list of booleans in the adapter and then access that using the position instead? – Tyler V Jan 05 '22 at 05:29
  • @TylerV yes I can. So there is no way to cancel onCreatViewViewHolde after notifyDataSetChanged? – GHH Jan 05 '22 at 05:38
  • Not that I know of - and I'm not sure you would want to, I don't think there is any guarantee that a holder gets bound to the same spot in the list. – Tyler V Jan 05 '22 at 05:45

0 Answers0