3

我是 android 新手,我正在尝试检测滑动事件。如果我将我的侦听器添加到 webview 或 textview 它工作得很好:

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

但是,如果我创建一个包含一些项目的线性布局,然后尝试添加我的侦听器,则不会发生:

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>

如果我滑动布局的实际儿童,即我的单选按钮或微调器,则将检测到滑动事件。但是,如果我在布局的空白处滑动,什么也不会发生

任何帮助和指示将不胜感激。提前致谢!

4

1 回答 1

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;
                }
            });
于 2013-04-04T10:39:21.447 回答