jmartinesp / SwissKnife

A multi-purpose library containing view injection and threading for Android using annotations
Apache License 2.0
254 stars 24 forks source link

Feature request: inject views without declaration #34

Open tobykurien opened 9 years ago

tobykurien commented 9 years ago

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 reference R.id.my_text_view in the activity code as myTextView.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.

jmartinesp commented 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.

tobykurien commented 9 years ago

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 :)

jmartinesp commented 9 years ago

If I'm not mistaken @mariogarcia wanted to do something similar to this. It was discussed on SK's gitter chat.