2012-04-23 9 views
2

ScrollViewの子をクリックしたときにコンテナでトリガされたonTouchイベントを取得できません。ScrollViewの子に触れるとビューのonTouchがトリガーされない

私はGalaxyタブでAPI 12を使用しています10.1。

誰でも助けることができますか?

おかげ

活動

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

uはOnClickListener ONEを作成しようとしましたレイアウトそれをすべての子に追加しますか? ScrollViewは、それが中にクリックする独自の領域を持っていないスクロール可能チャイルズ作るとして 多分これはあなたの を解決することができます。 (私が間違っている場合は^^私を修正)

+0

を必要としています。 – jul

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; 
} 
関連する問題