From de5430322d7873f57410467c41573f2e1d7db7e2 Mon Sep 17 00:00:00 2001 From: wada811 Date: Thu, 22 Apr 2021 23:42:31 +0900 Subject: [PATCH] Separate viewBinding method --- .../viewbinding/ActivityViewBinding.kt | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/ViewBinding-ktx/src/main/java/com/wada811/viewbinding/ActivityViewBinding.kt b/ViewBinding-ktx/src/main/java/com/wada811/viewbinding/ActivityViewBinding.kt index f92a7df..c626f82 100644 --- a/ViewBinding-ktx/src/main/java/com/wada811/viewbinding/ActivityViewBinding.kt +++ b/ViewBinding-ktx/src/main/java/com/wada811/viewbinding/ActivityViewBinding.kt @@ -7,15 +7,27 @@ import android.view.ViewGroup import androidx.fragment.app.FragmentActivity import androidx.viewbinding.ViewBinding -inline fun FragmentActivity.viewBinding( - crossinline bind: (View) -> T = { - T::class.java.getMethod("bind", View::class.java).invoke(null, it) as T +inline fun FragmentActivity.viewBinding(): Lazy { + return lazy(LazyThreadSafetyMode.NONE) { + val bind: (View) -> T = { + T::class.java.getMethod("bind", View::class.java).invoke(null, it) as T + } + val getContentView: FragmentActivity.() -> View = { + checkNotNull(findViewById(android.R.id.content).getChildAt(0)) { + "Call setContentView or Use Activity's secondary constructor passing layout res id." + } + } + bind(getContentView()) } -): Lazy = lazy(LazyThreadSafetyMode.NONE) { - val getContentView: FragmentActivity.() -> View = { - checkNotNull(findViewById(android.R.id.content).getChildAt(0)) { - "Call setContentView or Use Activity's secondary constructor passing layout res id." +} + +fun FragmentActivity.viewBinding(bind: (View) -> T): Lazy { + return lazy(LazyThreadSafetyMode.NONE) { + val getContentView: FragmentActivity.() -> View = { + checkNotNull(findViewById(android.R.id.content).getChildAt(0)) { + "Call setContentView or Use Activity's secondary constructor passing layout res id." + } } + bind(getContentView()) } - bind(getContentView()) }