I have an Activity with a layout containing a custom ImageView and a few other views (a ListView and a couple other TextViews). Depending on where the user touches the ImageView, I need to change the contents of the ListView and the TextViews. I have an onTouchEvent listener working fine in the ImageView class, but somehow I need to signal the Activity class that the touch event happened in the ImageView and pass it some data…then the Activity can properly adjust all the other views.
I thought about trying to return “false” from the ImageView‘s onTouchEvent handler, thus letting the event matriculate up to the ImageView’s parent view, but I don’t think it has a parent view…just a parent ViewGroup (LinearLayout).
This feels like a design issue to me, but I’m not sure.
Thanks in advance for any suggestions.
I don’t think there’s a built-in way to do this. Write your image view class to all for a call-back to be registered after it changes its content. Then in the activity’s
onCreatemethod, retrieve the image view and register the activity as the call-back object. When the image view finishes handling the touch event, it can just invoke the call-back method.Here’s a sketch of how this might work:
Then just have your activity declared to implement
MyImageView.OnImageChangedListenerand callimageView.setOnImageChangedListener(this)after finding the image view (probably usingfindViewById).Obviously, you can save some typing by shortening the method names. More importantly, you can, if it would be useful, add additional arguments to the call-back method.