Open tobykurien opened 9 years ago
While this could be done, I prefer the current approach. It is more verbose, but it gives you a more flexible environment. The way you want it to be is similar to what AndroidAnnotations or Anko do, but not being able to inject views when you need them can lead to problems in some complex cases.
Also, if I'm not mistaken, @s0nerik 's BetterKnife does something similar to what you want.
Thanks. The option to use @InjectView
will still be there, but perhaps applying @InjectViews(R.layout....)
to the class (Activity/Fragment/ViewHolder/etc) can inject all the view widgets. Would be very useful and time-saving :)
If I'm not mistaken @mariogarcia wanted to do something similar to this. It was discussed on SK's gitter chat.
I am not familiar with Groovy AST transformations and it's capabilities, but would it be possible to parse the layout XML file during transformation, and then create getters (or fields) for each view found in the XML file? This would eliminate the need to add, for example
@InjectView TextView myTextView
, and simply be able to referenceR.id.my_text_view
in the activity code asmyTextView.text = "..."
For reference, this is implemented in the Xtendroid project (see https://github.com/tobykurien/Xtendroid/blob/master/Xtendroid/docs/index.md#activities-and-fragments) for activities, fragments, view holders, and custom views, eliminating a lot of boilerplate code and adding compile-time checking of view bindings and onClick handlers.