2013-04-04 127 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; 
       } 
      });