Thursday, 15 August 2013

Android - how to detect touch listener if view has click listener -



Android - how to detect touch listener if view has click listener -

i have basic layout linear layout, scrollview , imageview.

scrollview has registered ontouchlistener , imageview onclicklistener. if tap on imageview , pulling out of imageview, not see log "onclick".

if tap out of imageview (on scrollview) , pulling somewhere, see log "on touch". how can grab ontouch event on imageview, when pulling out of it?

here code:

imageview testbutton = (imageview) myview.findviewbyid(r.id.imageview1); testbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { log.d(tag, "onclick"); } }); scrollview scrollview = (scrollview) myview.findviewbyid(r.id.scrollview1); scrollview.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { log.d(tag, "ontouch"); homecoming false; } });

and here xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <scrollview android:id="@+id/scrollview1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/com_facebook_blue" android:scrollbaralwaysdrawverticaltrack="false" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/com_facebook_profile_picture_blank_square" /> </linearlayout> </scrollview> </linearlayout>

set ontouchlistener imageview well. when pull away touch view's touchable part fire touch event. onclick wont fired when ontouch cancelled(motionevent.action_cancel).you should not consume ontouch() hence homecoming false in ontouch()

android android-layout ontouchlistener

No comments:

Post a Comment