I whould like to create a scala Views helper for Android
Using this combination of trait and class
class ScalaView(view: View) {
def onClick(action: View => Any) =
view.setOnClickListener(new OnClickListener {
def onClick(view: View) {
action(view)
}
})
}
trait ScalaViewTrait {
implicit def view2ScalaView(view: View) =
new ScalaView(view)
}
I’m able to write onClick() like so
class MainActivity extends Activity with ScalaViewTrait {
//....
val textView = new TextView(this)
textView.onClick(v => v.asInstanceOf[TextView] setText ("asdas"))
}
My concern is that i want to avoid casting v to TextView
v will always be TextView if is applied to a TextView LinearLayout if applied to LinearLayout and so on.
Is there any way that v gets dynamic casted to whatever view is applied?
Just started with Scala and i need your help with this.
UPDATE
Solved see my answer below
I managed to do it.
Here is how
And now i can write onClick like this
vis actually aTextViewwhenonClickis applied to aTextViewPS. Maybe i’ll write a bunch of helpers and publish them on GitHub. <3 Scala