简体   繁体   English

linearlayout上的android设置ontouchlistener不起作用

[英]android setting ontouchlistener on linearlayout does not work

I am new to android and I'm trying to detect sliding events. 我是android的新手,我正在尝试检测滑动事件。 If I add my listener to a webview or textview it works perfectly fine: 如果我将我的监听器添加到webview或textview,它的工作原理非常好:

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

However if I create a linearlayout with some items in it and then try to add my listener nohing happens: 但是,如果我创建一个包含一些项目的linearlayout,然后尝试添加我的监听器,则会发生nohing:

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

As far as I know both WebView and LinearLayout extend the View class so setOnTouchListener should work on both of them just fine. 据我所知,WebView和LinearLayout都扩展了View类,因此setOnTouchListener应该可以正常工作。 My question is how do I get it working on my linear layout view? 我的问题是如何让它在我的线性布局视图上工作? I tried to set the listener for all children of the layout separately, but it's not what I'm looking for. 我试图分别为布局的所有孩子设置监听器,但这不是我正在寻找的。 If, for example, this is my xml: 例如,如果这是我的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>

Then sliding event will be detected if I slide the actual chidlren of the layout, ie my radio buttons or spinners. 如果我滑动布局的实际chidlren,即我的单选按钮或微调器,将检测滑动事件。 But if I slide on the empty space of the layout nothing happens 但是,如果我在布局的空白区域滑动没有任何反应

Any help and pointers will be highly appreciated. 任何帮助和指示将受到高度赞赏。 Thanks in advance! 提前致谢!

try this listener 试试这个听众

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;
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM