-
Notifications
You must be signed in to change notification settings - Fork 55
Add AppCompat inflation inject sample #69
Comments
(Someone reminded me of this at the talk. Thanks, person!) |
I did the following and it seems to be working okay class InflationInjectContextWrapper constructor(
newBase: Context,
private val inflationInjectFactory: InflationInjectFactory
) : ContextWrapper(newBase) {
private var inflater: LayoutInflater? = null
override fun getSystemService(name: String): Any? {
if (LAYOUT_INFLATER_SERVICE == name) {
if (inflater == null) {
inflater = LayoutInflater.from(baseContext)
.cloneInContext(this)
.apply {
factory = FactoryWrapper(factory, inflationInjectFactory)
}
}
return inflater!!
}
return super.getSystemService(name)
}
private class FactoryWrapper(
private val factory: LayoutInflater.Factory?,
private val inflationInjectFactory: InflationInjectFactory
) : LayoutInflater.Factory {
override fun onCreateView(
name: String,
context: Context,
attrs: AttributeSet?
): View? {
return inflationInjectFactory.onCreateView(name, context, attrs)
?: factory?.onCreateView(name, context, attrs)
}
}
} |
Any updates? I get
when setting Layout Factory in AppCompatActivity. |
@dhruvj Did you try the sample that I posted above? The problem with using the factory with app compat is there as already a factory set on the inflater by default, which is what allows the app compat version of certain widgets to be inflated instead of their standard counterpart. e.g., |
@abyrnes Yes, your solution is working perfectly, although I believe it would be helpful to demonstrate to newcomers where and how to use
If you have a BaseActivity, then you can put it there instead and forget about it in subclasses. You do not need to override anything in fragments, since they contain a reference to a host activity and will use the shared inflater. Hope it helps someone. |
With test. I have this working in a side-project, but we should show it here and write a test to make sure it continues working.
The text was updated successfully, but these errors were encountered: