Skip to content

Latest commit

 

History

History
79 lines (57 loc) · 2.5 KB

README.md

File metadata and controls

79 lines (57 loc) · 2.5 KB

BindingExtension

CircleCI jitpack

ViewBinding is an amazing tool for Android but it's not so fit in Android development as we still have to do some config. BindingExtension is built to provide a simpler usage.

Install

Add Jitpack repository to your root build.grable:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Then add dependency in your module build.gradle:

dependencies {
  implementation 'com.github.jintin:BindingExtension:2.0.0'
}

Usage

Activity

Extend from BindingActivity with your actual ViewBinding inflate method reference then you can use binding directly after calling super.onCreate(savedInstanceState) and you don't have to call setContentView anymore:

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.label.setText(R.string.activity_label)
    }
}

Fragment

Extend from BindingFragment with your actual ViewBinding inflate method reference then you can use binding directly after super.onCreateView(inflater, container, savedInstanceState) is called:

class MainFragment : BindingFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.button.setOnClickListener {
            binding.button.setText(R.string.fragment_label)
        }
    }
}

ViewHolder

Extend from BindingHolder with parent ViewGroup and your actual ViewBinding inflate method reference then you can use binding directly:

class ViewHolder(parent: ViewGroup) :
    BindingHolder<AdapterMainBinding>(parent, AdapterMainBinding::inflate) {

    fun bind(data: String) {
        binding.name.text = data
    }
}

You can go to ./app module for more information.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Jintin/BindingExtension.

Buy Me A Coffee