After more than a week of persistently trying, I came up with an elegant solution:
In the XML, I used data binding:
<data>
<variable
name="shape"
type="chat.rocket.android.helper.Constants"/>
</data>
...
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
app:roundedCornerRadius='@{shape.AVATAR_SHAPE_CIRCLE ? @dimen/circle_avatar_corner_radius : @dimen/square_avatar_corner_radius}' />
As roundedCornerRadius
does not have a corresponding setRoundedCornerRadiusIn BindingAdapters.kt
,
@BindingAdapter("roundedCornerRadius")
fun setRoundedCornerRadius(view: SimpleDraweeView, height: Float) {
val roundingParams = RoundingParams.fromCornersRadius(height)
view.hierarchy.roundingParams = roundingParams
}
The Constants.kt
file has the constant which can be configured to change the shape:
object Constants {
...
const val AVATAR_SHAPE_CIRCLE = true
}
In RecyclerViewAdapter
,
...
val layoutInflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_contact, parent, false)
ContactViewHolder(binding.root)
...
This is the best solution I could come up with. It would be great if there is a way to apply multiple attributes depending on the value of the shape.AVATAR_SHAPE_CIRCLE
. Eg.
if(shape.AVATAR_SHAPE_CIRCLE)
apply these attr
else
apply these attr
Suggestions are welcome!