2012-04-23 136 views
2

当点击其ScrollView子项时,无法在容器上触发onTouch事件。onTouch在触摸其ScrollView子项时未触发的视图

我在Galaxy选项卡10.1上使用API​​ 12。

有人可以帮忙吗?

感谢

活动

public class TestActivity extends Activity{ 

    @Override 
    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.dude); 

     LinearLayout mylayout = (LinearLayout) findViewById(R.id.mylayout);   

     mylayout.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View view, MotionEvent event) { 

       // Only called when touched outside the ScrollView 

       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        /* do stuff */ 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        /* do stuff */ 
       } 
       return true; 
      } 
     });   
    } 
} 

布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mylayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:gravity="center" 
     android:text="Touch here trigger parent&apos;s onTouch" 
     android:textColor="#000000" 
     android:textSize="40sp" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="1000dp" 
       android:background="#b00000" 
       android:gravity="center" 
       android:text="Touch here DOES NOT trigger parent&apos;s onTouch" 
       android:textColor="#000000" 
       android:textSize="40sp" /> 
     </RelativeLayout> 
    </ScrollView> 

</LinearLayout> 

enter image description here

回答

0

难道ü尝试创建ONE OnClickListener一个nd将它添加到所有的孩子? 也许这可以解决您的 由于滚动型使得孩子的滚动它不会有一个自己的区域中单击。 (纠正我,如果我错了^^)

+0

这似乎是痛苦的。 – jul 2012-04-26 15:18:59

1
mylayout.setOnTouchListener(new OnTouchListener() { 
      public boolean onTouch(View view, MotionEvent event) { 

       // Only called when touched outside the ScrollView 

       if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) { 
        /* do stuff */ 
        return true; 
       } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) { 
        /* do stuff */ 
        return true; 
       } 
       return false; 
      } 
     }); 

这应该工作..但是当你很费劲的时候你不再有自动滚动功能......这将是一个只有拖动的粘滞滚动条。

0

你可能需要此代码

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) 
{ 
onTouchEvent(ev); 
return false; 
} 
相关问题