I have a ViewHolder that looks something like this:
class ViewHolder(
itemView: View,
var mSettingsRadioButton: RadioButton = itemView.findViewById(...),
var mSettingsCheckBox: CheckBox = itemView.findViewById(...),
var mSettingsChevron: Icon = itemView.findViewById(...)
) : RecyclerView.ViewHolder(itemView)
The idea is to make the the other two invisible when one of them is active, i.e. if a checkbox is active, the chevron and radio button should be invisible.
Right now I am using 3 functions for this purpose something like this:
fun setCheckbox(viewHolder: ViewHolder) {
viewHolder.apply {
mSettingsChevron.visibility = GONE
mSettingsRadioButton.visibility = GONE
mSettingsCheckBox.let {
it.visibility = VISIBLE
it.isChecked = false
}
}
}
Although this approach works, I have 3 functions that are practically doing the same thing. I am looking for much cleaner and optimized way of writing this functionality. Any suggestion is much appreciated. Thanks!!
If you want to use only one function you could, and it would remove redundant code so you would be achieving DRY principle.
And now you would just call
setVisibilityaccordingly