bhatnagarm
01/22/2020, 1:54 PMaipok
01/22/2020, 2:16 PM@BindingAdapter("dynamic_width")
fun View.setWidth(layoutParams: LayoutParams) {
this.layoutParams.width = when(layoutParams) {
LayoutParams.wrapContent -> ViewGroup.LayoutParams.WRAP_CONTENT
LayoutParams.matchParent -> ViewGroup.LayoutParams.MATCH_PARENT
}
}
@BindingAdapter("dynamic_height")
fun View.setHeight(layoutParams: LayoutParams) {
this.layoutParams.height = when(layoutParams) {
LayoutParams.wrapContent -> ViewGroup.LayoutParams.WRAP_CONTENT
LayoutParams.matchParent -> ViewGroup.LayoutParams.MATCH_PARENT
}
}
enum class LayoutParams{
wrapContent, matchParent
}
Usage will be like this:
app:dynamic_width="@{someCondition ? matchParent: wrapContent}"
bhatnagarm
01/23/2020, 3:47 AMaipok
01/23/2020, 6:37 AMandroid:layout_width="wrap_content"
and android:layout_height="wrap_content"
or whatever value is good to set as default for you and after that the BindingAdapter will override it. I'm sure this is working. As you saw probably for dynamic height example (at google data biunding article), you should define the default width and height, since they are required. And data binding executed later.bhatnagarm
01/23/2020, 6:43 AM