2013-04-04 197 views
3

我是新来的android和我试图检测滑动事件。如果我将监听器添加到网页视图或TextView的它精细完美的作品:如果我创建一个含有一些项的LinearLayoutAndroid设置ontouchlistener在linearlayout不起作用

WebView myWebView = (WebView) findViewById(R.id.myWebview);   
myWebView.setOnTouchListener(myListener); 

然而,然后尝试添加自己的听众nohing情况:

LinearLayout ll = (LinearLayout)findViewById(R.id.myLinearLayout); 
ll.setOnTouchListener(myListener); 

据我所知,WebView和LinearLayout扩展了View类,因此setOnTouchListener应该可以在它们两个上工作。我的问题是如何让它在我的线性布局视图上工作?我试图单独设置布局的所有孩子的监听器,但这不是我要找的。如果,例如,这是我的xml:

<LinearLayout 
     android:id="@+id/myLinearLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="RadioButton" /> 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="RadioButton" /> 

     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

      <Spinner 
       android:id="@+id/spinner1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

      <Spinner 
       android:id="@+id/spinner2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" /> 

     </LinearLayout> 

</LinearLayout> 

然后,如果我滑动布局的实际chidlren滑动事件将被检测到,即我的单选按钮或微调。但如果我在布局的空白区域滑动,则不会发生任何事情

任何帮助和指针都将受到高度赞赏。提前致谢!

回答

0

试听这位听众

LinearLayout l=(LinearLayout)findViewById(R.id.myLinearLayout); 


      l.setOnTouchListener(new OnTouchListener() { 

       @Override 
       public boolean onTouch(View arg0, MotionEvent arg1) { 
        // TODO Auto-generated method stub 
        Log.i("Touch","Sample x"+arg1.getX()); 
        Log.i("Touch","Sample x"+arg1.getY()); 
        return true; 
       } 
      });