Sunday, 20 September 2020

Won't notifyDataSetChanged() launch init{ } in RecyclerView.ViewHolder in Android Studio?

I'm learning RecyclerView with ListAdapter.

1: I find setControl() in init{ } isn't be launched after I run notifyDataSetChanged() , why?

2: What code should I place it in init{ }? What code should I place it in fun bind(aMVoice: MVoice{ } ?

Code

class VoiceAdapters (private val aHomeViewModel: HomeViewModel):
        ListAdapter<MVoice, VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
        return VoiceViewHolder(
            LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun onBindViewHolder(holder: VoiceViewHolder, position: Int) {
        val aMVoice = getItem(position)
        holder.bind(aMVoice)
    }

    inner class VoiceViewHolder (private val binding: LayoutVoiceItemBinding):
          RecyclerView.ViewHolder(binding.root) {

       
        init {
            setControl()
        }

        fun bind(aMVoice: MVoice) {
            binding.amVoice = aMVoice                
            binding.executePendingBindings()
        }

       
        fun setControl(){    
           binding.aHomeViewModel = aHomeViewModel    
           binding.chSelect.setOnCheckedChangeListener{ _, isChecked ->
                binding.amVoice?.let {
                   ...
                }
            }
             
            ...
         }

    }
}

Added Content:

To ADM: Thank you very much!

A: Why isn't it a good idea to passing a ViewModel to adapter ?

B: How can I use interface instead ? Could you show me some sample code?

BTW, the following item layout of RecyclerView need to use ViewModel aHomeViewModel to control whether the CheckBox chSelect is shown or not. I will set the value of aHomeViewModel.displayCheckBox in a fragment.

layout_voice_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <import type="android.view.View" />

    <variable name="aMVoice"
        type="info.dodata.voicerecorder.model.MVoice"  />
   
    <variable name="aHomeViewModel"
        type="info.dodata.voicerecorder.viewcontrol.HomeViewModel" />
</data>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/chSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="@{aHomeViewModel.displayCheckBox? View.VISIBLE: View.GONE}"
        android:text="" />

    <TextView
       android:id="@+id/voiceID"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{Integer.toString(aMVoice.id)}" />

</LinearLayout>
</layout>


from Won't notifyDataSetChanged() launch init{ } in RecyclerView.ViewHolder in Android Studio?

No comments:

Post a Comment