kotterknife icon indicating copy to clipboard operation
kotterknife copied to clipboard

Bindings to custom container based on ViewContainer

Open pablow91 opened this issue 10 years ago • 0 comments

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 DialogFragment class and create Dialog object on onCreateDialog method. It cause exception, because DialogFragment.getView return null instead of dialog view.
  • Creating optimal ListAdapter might be difficult with Kotterknife. To create optimal ListAdapter I 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.

pablow91 avatar Jan 05 '15 18:01 pablow91