2014-06-24 150 views
0

我有线性布局,ScrollView和ImageView的基本布局。Android - 如果视图有点击监听器,如何检测触摸监听器

ScrollView已注册onTouchListener和imageview onClicklistener。 如果我点击ImageView并且我拔出ImageView,我看不到具有“onclick”的日志。

如果我点击imageView(在scrollView上)并且我正在拉某处,我会看到“on touch”的日志。 当我拔出它时,如何捕捉imageView上的onTouch事件?

下面是代码:

ImageView testButton = (ImageView) myView.findViewById(R.id.imageView1); 
testButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

     Log.d(tag, "onclick"); 

    } 
}); 

ScrollView scrollView = (ScrollView) myView.findViewById(R.id.scrollView1); 
scrollView.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     Log.d(tag, "ontouch"); 

     return false; 
    } 
}); 

这里是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/com_facebook_blue" 
     android:scrollbarAlwaysDrawVerticalTrack="false" > 

     <LinearLayout 
      android:id="@+id/LinearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/com_facebook_profile_picture_blank_square"  /> 

     </LinearLayout> 


    </ScrollView> 

</LinearLayout> 

回答

0

将其设置为滚动视图:

ScrollView mainScroll=(ScrollView) findViewById(R.id.your_scroll_view) 
mainScroll.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 
mainScroll.setFocusable(true); //If you need this 
mainScroll.setFocusableInTouchMode(true); //If you need this 

然后你imageViewonclick监听器将被触发。此代码也可能会严重影响您的功能的其他部分。因此,请在实施此操作后检查视图,如果发生任何屏幕跳跃,请回复。

0

OnTouchListener也设置为ImageView。当您从视图的可触摸区域拉开触摸时,它将触发Touch事件。当ontouch被取消时,OnClick不会被触发(MotionEvent.ACTION_CANCEL)。您不应该使用onTouch(),因此在onTouch()中返回false