| package com.airbnb.lottie.samples.utils |
| |
| import android.view.LayoutInflater |
| import android.view.ViewGroup |
| import androidx.viewbinding.ViewBinding |
| import kotlin.properties.ReadOnlyProperty |
| import kotlin.reflect.KProperty |
| |
| /** |
| * Create bindings for a view similar to bindView. |
| * |
| * To use, just call: |
| * private val binding: FHomeWorkoutDetailsBinding by viewBinding() |
| * with your binding class and access it as you normally would. |
| */ |
| inline fun <reified T : ViewBinding> ViewGroup.viewBinding() = ViewBindingDelegate(T::class.java, this) |
| |
| class ViewBindingDelegate<T : ViewBinding>( |
| private val bindingClass: Class<T>, |
| val view: ViewGroup |
| ) : ReadOnlyProperty<ViewGroup, T> { |
| private var binding: T? = null |
| |
| override fun getValue(thisRef: ViewGroup, property: KProperty<*>): T { |
| binding?.let { return it } |
| |
| @Suppress("UNCHECKED_CAST") |
| binding = try { |
| val inflateMethod = bindingClass.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java) |
| inflateMethod.invoke(null, LayoutInflater.from(thisRef.context), thisRef) |
| } catch (e: NoSuchMethodException) { |
| val inflateMethod = bindingClass.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java) |
| inflateMethod.invoke(null, LayoutInflater.from(thisRef.context), thisRef, true) as T |
| } as T |
| return binding!! |
| } |
| } |