I am new to android and I'm trying to detect sliding events. If I add my listener to a webview or textview it works perfectly fine:
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 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. 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:
<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. 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;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.