Created
July 29, 2020 05:09
-
-
Save Bruno125/f2a11c4975803fada1850230a5f52eb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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