### FooAdapter usage sample for BaseRecyclerViewAdapterHelper Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingQuickAdapter with a generated binding class. ```kotlin class FooAdapter : BaseBindingQuickAdapter() { override fun convert(holder: BaseBindingHolder, item: Foo) { holder.getViewBinding().apply { tvFoo.text = item.value } } } ``` -------------------------------- ### MainActivity usage sample Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingActivity with a generated binding class. ```kotlin class MainActivity : BaseBindingActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### HomeFragment usage sample Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingFragment with a generated binding class. ```kotlin class HomeFragment: BaseBindingFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### FooViewDelegate usage sample for MultiType Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BindingViewDelegate with a generated binding class. ```kotlin class FooViewDelegate : BindingViewDelegate() { override fun onBindViewHolder(holder: BindingViewHolder, item: Foo) { holder.binding.apply { tvFoo.text = item.value } } } ``` -------------------------------- ### FooAdapter usage sample (BaseRecyclerViewAdapterHelper) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingQuickAdapter with a specific ViewBinding class. In convert, it retrieves the binding and sets text on a view. ```Kotlin class FooAdapter : BaseBindingQuickAdapter(ItemFooBinding::inflate) { override fun convert(holder: BaseBindingHolder, item: Foo) { holder.getViewBinding().apply { tvFoo.text = item.value } } } ``` -------------------------------- ### MainActivity usage sample Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingActivity with a specific ViewBinding class. Overrides onCreate to set text on a binding view. ```Kotlin class MainActivity : BaseBindingActivity(ActivityMainBinding::inflate) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### HomeFragment usage sample Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BaseBindingFragment with a specific ViewBinding class. Overrides onViewCreated to set text on a binding view. ```Kotlin class HomeFragment: BaseBindingFragment(FragmentHomeBinding::inflate) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### FooViewDelegate usage sample (MultiType) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Example of using BindingViewDelegate with a specific ViewBinding class. In onBindViewHolder, it uses the binding to set text on a view. ```Kotlin class FooViewDelegate : BindingViewDelegate(ItemFooBinding::inflate) { override fun onBindViewHolder(holder: BindingViewHolder, item: Foo) { holder.binding.apply { tvFoo.text = item.value } } } ``` -------------------------------- ### Use binding extension in Fragment (reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Shows how to use the binding() extension function with reflection in a Fragment. The constructor requires the layout ID. The example also demonstrates binding a child view with Method.INFLATE. ```kotlin class HomeFragment : Fragment(R.layout.fragment_home) { private val binding: FragmentHomeBinding by binding() private val childBinding: LayoutChildBinding by binding(Method.INFLATE) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.container.addView(childBinding.root) } } ``` -------------------------------- ### Java: Get ViewBinding in BRVAH adapter Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/brvah In a Java BaseQuickAdapter, use BaseViewHolderUtil.getBinding(holder, ItemFooBinding::bind) to access the ViewBinding and set text on a view. ```java public class FooAdapter extends BaseQuickAdapter { public FooAdapter() { super(R.layout.item_foo); } @Override protected void convert(@NotNull BaseViewHolder holder, Foo foo) { ItemFooBinding binding = BaseViewHolderUtil.getBinding(holder, ItemFooBinding::bind); binding.tvFoo.setText(foo.getValue()); } } ``` -------------------------------- ### DialogFragment with Dialog binding (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Use reflection to bind a Dialog's layout. The inner LoadingDialog uses by binding() to get DialogLoadingBinding. Sets window layout to MATCH_PARENT/WRAP_CONTENT and updates tvMessage. ```Kotlin class LoadingDialogFragment(private val text: String? = null) : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { isCancelable = false return LoadingDialog(requireContext()) } inner class LoadingDialog(context: Context) : Dialog(context, R.style.DialogTheme) { private val binding: DialogLoadingBinding by binding() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) window?.setLayout(MATCH_PARENT, WRAP_CONTENT) binding.tvMessage.text = "Wait a minute..." } } } ``` -------------------------------- ### Use binding extension in Fragment (nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Shows how to use the binding() extension function without reflection in a Fragment. The constructor requires the layout ID. The example also demonstrates binding a child view using a lambda with LayoutChildBinding.inflate. ```kotlin class HomeFragment : Fragment(R.layout.fragment_home) { private val binding by binding(FragmentHomeBinding::bind) private val childBinding by binding { LayoutChildBinding.inflate(layoutInflater) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.container.addView(childBinding.root) } } ``` -------------------------------- ### Custom ViewHolder with getBinding (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview For custom ViewHolder like BRVAH, use holder.getBinding() to access the binding object. This example uses reflection. ```Kotlin class FooAdapter : BaseQuickAdapter(R.layout.item_foo) { override fun convert(holder: BaseViewHolder, item: Foo) { holder.getBinding().tvFoo.text = item.value } } ``` -------------------------------- ### Custom ViewHolder with getBinding (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview For custom ViewHolder like BRVAH, use holder.getBinding(ItemFooBinding::bind) to access the binding object. This example uses non-reflection. ```Kotlin class FooAdapter : BaseQuickAdapter(R.layout.item_foo) { override fun convert(holder: BaseViewHolder, item: Foo) { holder.getBinding(ItemFooBinding::bind).tvFoo.text = item.value } } ``` -------------------------------- ### Configure ViewBinding and add dependencies Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/?id=gradle Enable the ViewBinding build feature and add the optional ViewBindingKTX dependencies. Choose the ones you need based on your usage. ```groovy android { buildFeatures { viewBinding true } } dependencies { // The following are optional, please add as needed implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-ktx:2.1.0' implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-nonreflection-ktx:2.1.0' implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-base:2.1.0' implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-brvah:2.1.0' } ``` -------------------------------- ### BaseBindingQuickAdapter base class for BaseRecyclerViewAdapterHelper Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Base adapter class for BaseRecyclerViewAdapterHelper using reflection to inflate ViewBinding. ```kotlin abstract class BaseBindingQuickAdapter(layoutResId: Int = -1) : BaseQuickAdapter(layoutResId) { override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int) = BaseBindingHolder(ViewBindingUtil.inflateWithGeneric(this, parent)) class BaseBindingHolder(private val binding: ViewBinding) : BaseViewHolder(binding.root) { constructor(itemView: View) : this(ViewBinding { itemView }) @Suppress("UNCHECKED_CAST") fun getViewBinding() = binding as VB } } ``` -------------------------------- ### PopupWindow delegate (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Creates a PopupWindow using the popupWindow delegate with reflection. The lambda provides the binding, allowing direct access to views like btnLike. ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` -------------------------------- ### Add JitPack repository to root build.gradle Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/?id=gradle Add the JitPack repository to the repositories block in the root build.gradle file to enable dependency resolution for ViewBindingKTX. ```groovy allprojects { repositories { // ... maven { url 'https://www.jitpack.io' } } } ``` -------------------------------- ### popupWindow delegate Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=fragment Creates a PopupWindow using ViewBinding. The lambda provides the binding to configure the popup content. ```APIDOC ## popupWindow delegate ### Description Creates a PopupWindow using ViewBinding. The lambda provides the binding to configure the popup content. ### Method Property delegate ### Parameters - **binding** (ViewBinding) - The ViewBinding instance for the popup content. - **init** (lambda) - A lambda to configure the binding. ### Usage Example ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` ``` -------------------------------- ### DialogFragment with Dialog binding (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Use non-reflection to bind a Dialog's layout. The inner LoadingDialog uses by binding(DialogLoadingBinding::inflate) to get the binding. Sets window layout to MATCH_PARENT/WRAP_CONTENT and updates tvMessage. ```Kotlin class LoadingDialogFragment(private val text: String? = null) : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { isCancelable = false return LoadingDialog(requireContext()) } inner class LoadingDialog(context: Context) : Dialog(context, R.style.DialogTheme) { private val binding by binding(DialogLoadingBinding::inflate) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) window?.setLayout(MATCH_PARENT, WRAP_CONTENT) binding.tvMessage.text = "Wait a minute..." } } } ``` -------------------------------- ### PopupWindow delegate Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions Creates a PopupWindow using ViewBinding via a property delegate. Reflection variant uses a lambda, non-reflection uses a binding class reference. ```APIDOC ## PopupWindow delegate ### Description Creates a PopupWindow using ViewBinding via a property delegate. Reflection variant uses a lambda, non-reflection uses a binding class reference. ### Method Property delegate for PopupWindow ### Parameters - **binding** (ViewBinding) - Required - The ViewBinding class or lambda to bind the view. - **init** (lambda) - Optional - Initialization block to configure the view. ### Usage Example Reflection: ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` Non-reflection: ```kotlin private val popupWindow by popupWindow(LayoutPopupBinding::inflate) { btnLike.setOnClickListener { ... } } ``` ``` -------------------------------- ### PopupWindow popupWindow Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=custom-view Creates a PopupWindow with a view binding layout. Supports both reflection and non-reflection modes. ```APIDOC ## PopupWindow popupWindow ### Description Creates a PopupWindow with a view binding layout. Supports both reflection and non-reflection modes. ### Method Property delegate for PopupWindow ### Parameters - **binding** (ViewBinding) - Required - The view binding class to use for the popup content. - **init** (lambda) - Required - Initialization block to configure the binding views. ### Usage Examples #### Reflection ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` #### Nonreflection ```kotlin private val popupWindow by popupWindow(LayoutPopupBinding::inflate) { btnLike.setOnClickListener { ... } } ``` ``` -------------------------------- ### BaseBindingQuickAdapter base class (BaseRecyclerViewAdapterHelper) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Abstract base class for RecyclerView adapters using BaseRecyclerViewAdapterHelper. It creates a BaseBindingHolder that wraps the ViewBinding and provides a method to retrieve it. ```Kotlin abstract class BaseBindingQuickAdapter( private val inflate: (LayoutInflater, ViewGroup, Boolean) -> VB, layoutResId: Int = -1 ) : BaseQuickAdapter(layoutResId) { override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int) = BaseBindingHolder(inflate(LayoutInflater.from(parent.context), parent, false)) class BaseBindingHolder(private val binding: ViewBinding) : BaseViewHolder(binding.root) { constructor(itemView: View) : this(ViewBinding { itemView }) @Suppress("UNCHECKED_CAST") fun getViewBinding() = binding as VB } } ``` -------------------------------- ### Add ViewBindingKTX dependency (reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Adds the viewbinding-ktx dependency for the reflection-based usage of ViewBindingKTX. ```groovy implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-ktx:2.1.0' ``` -------------------------------- ### Use binding extension in Activity (reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Shows how to use the binding() extension function with reflection in an Activity. The binding property is initialized lazily and used in onCreate. If the binding is not used in onCreate, you must call setContentView(binding.root). ```kotlin class MainActivity : AppCompatActivity() { private val binding: ActivityMainBinding by binding() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### Use binding extension in Activity (nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Shows how to use the binding() extension function without reflection in an Activity, passing the inflate function explicitly. If the binding is not used in onCreate, you must call setContentView(binding.root). ```kotlin class MainActivity : AppCompatActivity() { private val binding by binding(ActivityMainBinding::inflate) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding.tvHelloWorld.text = "Hello Android!" } } ``` -------------------------------- ### PopupWindow - popupWindow (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=use-extension-functions Creates a PopupWindow with a custom view using reflection-based binding. ```APIDOC ## PopupWindow - popupWindow (Reflection) ### Description Creates a PopupWindow with a custom view using reflection-based binding. ### Method popupWindow ### Parameters - **bindingClass** (KClass) - Required - The binding class to use for the popup content. - **init** (LayoutPopupBinding.() -> Unit) - Required - Lambda to configure the binding's views. ### Request Example ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` ### Response Returns a PopupWindow instance. ``` -------------------------------- ### BaseBindingActivity base class Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Base class for Activity using reflection to inflate ViewBinding. Override onCreate to set content view with binding.root. ```kotlin abstract class BaseBindingActivity : AppCompatActivity() { lateinit var binding: VB override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater) setContentView(binding.root) } } ``` -------------------------------- ### PopupWindow delegate (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Same as reflection variant but uses LayoutPopupBinding::inflate to avoid reflection. ```kotlin private val popupWindow by popupWindow(LayoutPopupBinding::inflate) { btnLike.setOnClickListener { ... } } ``` -------------------------------- ### PopupWindow popupWindow delegate Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=adapter Creates a PopupWindow with a custom ViewBinding layout. The reflection variant uses type inference, while the non-reflection variant requires an explicit binding inflater. ```APIDOC ## PopupWindow popupWindow delegate ### Description Creates a PopupWindow with a custom ViewBinding layout. The reflection variant uses type inference, while the non-reflection variant requires an explicit binding inflater. ### Usage #### Reflection ```kotlin private val popupWindow by popupWindow { btnLike.setOnClickListener { ... } } ``` #### Nonreflection ```kotlin private val popupWindow by popupWindow(LayoutPopupBinding::inflate) { btnLike.setOnClickListener { ... } } ``` ``` -------------------------------- ### Apache License 2.0 Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/?id=gradle The library is licensed under the Apache License, Version 2.0. This snippet contains the standard license header. ```text Copyright (C) 2020. Dylan Cai Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` -------------------------------- ### Custom View binding (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Use reflection to inflate the custom view's layout and a child layout with attachToParent = false. The child root is added to the container in init. ```Kotlin class CustomView(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) { private val binding = inflate() private val childBinding: LayoutChildBinding by binding(attachToParent = false) init { binding.container.addView(childBinding.root) } } ``` -------------------------------- ### BindingViewDelegate base class for MultiType Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Base delegate class for MultiType using reflection to inflate ViewBinding. ```kotlin abstract class BindingViewDelegate : ItemViewDelegate>() { override fun onCreateViewHolder(context: Context, parent: ViewGroup) = BindingViewHolder(ViewBindingUtil.inflateWithGeneric(this, parent)) class BindingViewHolder(val binding: VB) : RecyclerView.ViewHolder(binding.root) } ``` -------------------------------- ### Add viewbinding-brvah dependency Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/brvah Add the viewbinding-brvah dependency to your project. Note that this dependency cannot be added if the viewbinding-nonreflection-ktx dependency is already used. ```groovy // If viewbinding-nonreflection-ktx dependency is used, it can not be added implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-brvah:2.1.0' ``` -------------------------------- ### Adapter with BindingViewHolder (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Use BindingViewHolder with reflection to avoid creating a custom ViewHolder class. The withBinding lambda sets click listeners on root and tvText. The DiffCallback compares String items. ```Kotlin class TextAdapter : ListAdapter>(DiffCallback()) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = BindingViewHolder(parent) .withBinding { root.setOnClickListener { ... } tvText.setOnClickListener { ... } } override fun onBindViewHolder(holder: BindingViewHolder, position: Int) { holder.binding.tvText.text = currentList[position] } class DiffCallback : DiffUtil.ItemCallback() { override fun areItemsTheSame(oldItem: String, newItem: String) = oldItem == newItem override fun areContentsTheSame(oldItem: String, newItem: String) = oldItem == newItem } } ``` -------------------------------- ### Add ViewBindingKTX dependency (nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Adds the viewbinding-nonreflection-ktx dependency for the non-reflection usage of ViewBindingKTX. ```groovy implementation 'com.github.DylanCaiCoding.ViewBindingKTX:viewbinding-nonreflection-ktx:2.1.0' ``` -------------------------------- ### TabLayout setCustomView and doOnCustomTabSelected Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=get-started Customize TabLayout tabs with view binding using setCustomView and observe tab selection events with doOnCustomTabSelected. Both reflection and non-reflection variants are shown. ```APIDOC ## TabLayout setCustomView and doOnCustomTabSelected ### Description Customize TabLayout tabs with view binding using setCustomView and observe tab selection events with doOnCustomTabSelected. Both reflection and non-reflection variants are shown. ### Method Extension functions on TabLayout ### Usage Examples #### Reflection ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() tabLayout.doOnCustomTabSelected( onTabSelected = { tvTitle.textSize = 12f }, onTabUnselected = { tvTitle.textSize = 10f }) ``` #### Nonreflection ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView(LayoutBottomTabBinding::bind) { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() tabLayout.doOnCustomTabSelected(LayoutBottomTabBinding::bind, onTabSelected = { tvTitle.textSize = 12f }, onTabUnselected = { tvTitle.textSize = 10f }) ``` ``` -------------------------------- ### BaseBindingActivity base class Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Abstract base class for Activities that uses ViewBinding. It takes an inflate function in the constructor and sets the content view to binding.root in onCreate. ```Kotlin abstract class BaseBindingActivity( private val inflate: (LayoutInflater) -> VB ) : AppCompatActivity() { lateinit var binding: VB override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = inflate(layoutInflater) setContentView(binding.root) } } ``` -------------------------------- ### PopupWindow - popupWindow (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=use-extension-functions Creates a PopupWindow with a custom view using a binding inflater function. ```APIDOC ## PopupWindow - popupWindow (Nonreflection) ### Description Creates a PopupWindow with a custom view using a binding inflater function. ### Method popupWindow ### Parameters - **inflater** (LayoutInflater.() -> LayoutPopupBinding) - Required - Function to inflate the binding. - **init** (LayoutPopupBinding.() -> Unit) - Required - Lambda to configure the binding's views. ### Request Example ```kotlin private val popupWindow by popupWindow(LayoutPopupBinding::inflate) { btnLike.setOnClickListener { ... } } ``` ### Response Returns a PopupWindow instance. ``` -------------------------------- ### TabLayout.setCustomView Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions Sets a custom view for a TabLayout tab using ViewBinding. Reflection variant uses a lambda to bind the view, while non-reflection variant uses a binding class reference. ```APIDOC ## TabLayout.setCustomView ### Description Sets a custom view for a TabLayout tab using ViewBinding. The reflection variant uses a lambda to bind the view, while the non-reflection variant uses a binding class reference. ### Method Extension function on TabLayout.Tab ### Parameters - **binding** (ViewBinding) - Required - The ViewBinding class or lambda to bind the view. - **init** (lambda) - Optional - Initialization block to configure the view. ### Usage Example Reflection: ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() ``` Non-reflection: ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView(LayoutBottomTabBinding::bind) { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() ``` ``` -------------------------------- ### TabLayout with custom view (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Uses TabLayoutMediator with a custom view binding via reflection. The setCustomView lambda provides the binding, allowing direct access to views like tvTitle and ivIcon. Also demonstrates doOnCustomTabSelected to adjust text size on tab selection. ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() tabLayout.doOnCustomTabSelected( onTabSelected = { tvTitle.textSize = 12f }, onTabUnselected = { tvTitle.textSize = 10f }) ``` -------------------------------- ### TabLayout - setCustomView (Reflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=use-extension-functions Sets a custom view for a TabLayout tab using reflection-based binding. The lambda provides direct access to the binding's views. ```APIDOC ## TabLayout - setCustomView (Reflection) ### Description Sets a custom view for a TabLayout tab using reflection-based binding. The lambda provides direct access to the binding's views. ### Method setCustomView ### Parameters - **bindingClass** (KClass) - Required - The binding class to use for the custom view. - **init** (LayoutBottomTabBinding.() -> Unit) - Required - Lambda to configure the binding's views. ### Request Example ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() ``` ### Response No return value. ``` -------------------------------- ### BindingViewDelegate base class (MultiType) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Abstract base class for MultiType item view delegates. It creates a BindingViewHolder that holds the ViewBinding and uses it as the item view. ```Kotlin abstract class BindingViewDelegate ( private val inflate: (LayoutInflater, ViewGroup, Boolean) -> VB ): ItemViewDelegate>() { override fun onCreateViewHolder(context: Context, parent: ViewGroup) : BindingViewHolder = BindingViewHolder(inflate(LayoutInflater.from(parent.context), parent, false)) class BindingViewHolder(val binding: VB) : RecyclerView.ViewHolder(binding.root) } ``` -------------------------------- ### TabLayout.setCustomView Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=fragment Sets a custom view for a TabLayout tab using ViewBinding. The lambda provides the binding to configure the view. ```APIDOC ## TabLayout.setCustomView ### Description Sets a custom view for a TabLayout tab using ViewBinding. The lambda provides the binding to configure the view. ### Method Extension function on TabLayout.Tab ### Parameters - **binding** (ViewBinding) - The ViewBinding instance for the custom view. - **init** (lambda) - A lambda to configure the binding. ### Usage Example ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() ``` ``` -------------------------------- ### Custom View binding (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Use non-reflection to inflate the custom view's layout and a child layout with attachToParent = false. The child root is added to the container in init. ```Kotlin class CustomView(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) { private val binding = inflate(LayoutCustomViewBinding::inflate) private val childBinding by binding(LayoutChildBinding::inflate, attachToParent = false) init { binding.container.addView(childBinding.root) } } ``` -------------------------------- ### TabLayout with custom view (Nonreflection) Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/extensions?id=navigationview Same as reflection variant but uses LayoutBottomTabBinding::bind to avoid reflection. The setCustomView and doOnCustomTabSelected accept the binding function as a parameter. ```kotlin TabLayoutMediator(tabLayout, viewPager2) { tab, position -> tab.setCustomView(LayoutBottomTabBinding::bind) { tvTitle.setText(titleList[position]) tvTitle.textSize = if (position == 0) 12f else 10f ivIcon.setImageResource(iconList[position]) ivIcon.contentDescription = tvTitle.text.toString() } }.attach() tabLayout.doOnCustomTabSelected(LayoutBottomTabBinding::bind, onTabSelected = { tvTitle.textSize = 12f }, onTabUnselected = { tvTitle.textSize = 10f }) ``` -------------------------------- ### BaseBindingFragment base class Source: https://dylancaicoding.github.io/ViewBindingKTX/#/en/baseclass?id=don39t-use-reflection Abstract base class for Fragments that uses ViewBinding. It manages the binding lifecycle, setting _binding in onCreateView and nullifying it in onDestroyView to avoid leaks. ```Kotlin abstract class BaseBindingFragment( private val inflate: (LayoutInflater, ViewGroup?, Boolean) -> VB ) : Fragment() { private var _binding: VB? = null val binding: VB get() = _binding!! override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { _binding = inflate(inflater, container, false) return binding.root } override fun onDestroyView() { super.onDestroyView() _binding = null } } ```