Well I have two imageviews and they look like this. I want to add a TouchListener to just imageview1. but Listener not working properly. I am adding imageview2 to
android:focusable="false"
but not working again
Set a custom OnTouchListener
to imageview2
that always returns false in the onTouch
method. This way the touch event will be passed to the views bellow until one of them returns true in the onTouch
method.
Try this. It works for me correctly even when you click on imageview2. MainActivity onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.imageview1);
imageView1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("imageviewandontouchlistener", "imageView1 onTouch");
return false;
}
});
}
And in your Layout XML:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"/>
<ImageView
android:id="@+id/imageview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2" />
</FrameLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.