2014-03-24 56 views
2

enter image description here通過ImageView的Android ImageView OnTouchListener

那麼我有兩個imageviews,他們看起來像這樣。 我想添加TouchListener到imageview1。但聽衆不能正常工作。 我加入imageview2到

android:focusable="false" 

但設置自定義OnTouchListenerimageview2總是返回在onTouch方法不會再

回答

2

試試這個。即使點擊了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; 
     } 
    }); 
} 

而在你的佈局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> 
3

工作。通過這種方式,觸摸事件將傳遞到下面的視圖,直到其中一個返回trueonTouch方法中。