Skip to content

Instantly share code, notes, and snippets.

@Bruno125
Created July 29, 2020 05:09
Show Gist options
  • Save Bruno125/f2a11c4975803fada1850230a5f52eb9 to your computer and use it in GitHub Desktop.
Save Bruno125/f2a11c4975803fada1850230a5f52eb9 to your computer and use it in GitHub Desktop.
package com.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.viewbinding.ViewBinding
import com.test.databinding.ActivityMainBinding
abstract class BindingActivity<T: ViewBinding>(val name: String): AppCompatActivity(){
private val binding by lazy {
loadBinding<T>(name)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
with(binding) {
setContentView(binding.root)
setup(this)
}
}
abstract fun setup(binding: T)
}
private fun <T> AppCompatActivity.loadBinding(className: String): T {
return Class.forName("$packageName.databinding.$className")
.getMethod("inflate", LayoutInflater::class.java)
.invoke(null, layoutInflater) as T
}
class MainActivity : BindingActivity<ActivityMainBinding>("ActivityMainBinding") {
override fun setup(binding: ActivityMainBinding) {
binding.texto.text = "testtt"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment