Will asume you are using ConstraintLayout and also want to show the views (currently your code only hides the views) so I think it can be improved in 2 ways.
The first way is to use View.isVisible
extension from google's lib instead of setting GONE. So you code will become like this:
viewModel.shouldDisplayDischargeCurrent.observe(this, Observer { shouldDisplay ->
binding.textviewDischargeCurrentTitle.isVisible = !shouldDisplay
binding.textviewDischargeCurrent.isVisible = !shouldDisplay
binding.textviewDischargeTimeTitle.isVisible = !shouldDisplay
binding.textviewDischargeTime.isVisible = !shouldDisplay
})
and the second way is to use Constraint Group. Add a new element in XML with all the view's ids you want to hide:
<androidx.constraintlayout.widget.Group
android:id="@+id/textview_group"
app:constraint_referenced_ids="textviewDischargeCurrent, textviewDischargeCurrentTitle,textviewDischargeTime,textviewDischargeTimeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
and then in your code:
viewModel.shouldDisplayDischargeCurrent.observe(this, Observer { shouldDisplay ->
// those are no longer needed
// binding.textviewDischargeCurrentTitle.isVisible = !shouldDisplay
// binding.textviewDischargeCurrent.isVisible = !shouldDisplay
// binding.textviewDischargeTimeTitle.isVisible = !shouldDisplay
// binding.textviewDischargeTime.isVisible = !shouldDisplay
binding.textviewGroup.isVisible = !shouldDisplay
})
Also Observer {
can be removed so the final result with Constraint Group will be:
viewModel.shouldDisplayDischargeCurrent.observe(this) { shouldDisplay ->
binding.textviewGroup.isVisible = !shouldDisplay
}
Regarding performance I could not find much about this. Maybe someone else can comment about it. Personaly I would go with Constraint Group and View.isVisible