2012-04-23 76 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; 
}