Skip to content

Base classes which handle inflating of ViewBindings in activities, fragments and recyclerview adapters.

Notifications You must be signed in to change notification settings

mickverm/BaseViewBinding

Repository files navigation

Base ViewBinding

About

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

In most cases, view binding replaces findViewById.

This sample shows how ViewBinding can be used in base classes.

Usage

Activity

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
	setSupportActionBar(binding.toolbar)
    }

    ...
}

Fragment

class ItemsFragment : BaseFragment<FragmentItemsBinding>(FragmentItemsBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.tvTitle = "Hello World!"
    }

    ...
}

RecyclerView Adapter

class ItemsAdapter : BaseAdapter<ItemsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(parent.inflateBinding(ItemItemBinding::inflate))
    }
	
    ...
	
    class ViewHolder(private val binding: ItemItemBinding) : BaseViewHolder(binding) {

        fun bind(item: DummyItem) {
            binding.tvContent = item.content
        }

        ...

    }
}

About

Base classes which handle inflating of ViewBindings in activities, fragments and recyclerview adapters.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages