kotterknife
kotterknife copied to clipboard
Bindings to custom container based on ViewContainer
I have migrated one of my small project from Java to Kotlin. I replaced Butterknife with Kotterknife for view injections, but I was unable to proper inject views in two cases (there is probably much more):
- To create dialog I used
DialogFragmentclass and create Dialog object ononCreateDialogmethod. It cause exception, becauseDialogFragment.getViewreturn null instead of dialog view. - Creating optimal
ListAdaptermight be difficult withKotterknife. To create optimalListAdapterI would like to use this code (or something similar - I just started learning Kotlin):
val view: View = preView ?: LayoutInflater.from(context).inflate(R.layout.list_item, parent,false)
val c: Container = preView?.getTag() as Container? ?: Container(view)
if (preView == null) {
view.setTag(c);
}
c.item.setText(getItem(position))
return view
That is why I created ViewContainer class with can be used to solved both problems. In Butterknife I tend to use ButterKnife.inject(container, view) in such situations.