سؤال

enter image description here

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

هل كانت مفيدة؟

المحلول

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>

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top