[英]Android - how to detect touch listener if view has click listener
I have basic layout with linear layout, ScrollView and ImageView. 我有线性布局,ScrollView和ImageView的基本布局。
ScrollView has registered onTouchListener and imageview onClicklistener. ScrollView已注册onTouchListener和imageview onClicklistener。 If I tap on ImageView and I am pulling out of ImageView, I do not see log with "onclick".
如果我点击ImageView而退出ImageView,则看不到带有“ onclick”的日志。
If I tap out of imageView (on scrollView) and I am pulling somewhere, I see log with "on touch". 如果我从imageView上抽出(在scrollView上)并且拉到某处,则会看到带有“触摸”的日志。 How can I catch onTouch event on my imageView, when I am pulling out of it?
当我退出时,如何在imageView上捕获onTouch事件?
Here is the 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");
return false;
}
});
And here is xml: 这是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 this to your scroll view: 将此设置为滚动视图:
ScrollView mainScroll=(ScrollView) findViewById(R.id.your_scroll_view)
mainScroll.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mainScroll.setFocusable(true); //If you need this
mainScroll.setFocusableInTouchMode(true); //If you need this
Then your imageView
onclick
listener will be triggered. 然后,您的
imageView
onclick
侦听器将被触发。 This code could also substantially affect other parts of your functionality. 此代码也可能会严重影响功能的其他部分。 So please check the view once you implement this and revert if there are any screen jumps which occur.
因此,一旦实现此功能,请检查视图,如果出现任何屏幕跳转,请还原视图。
Set an OnTouchListener
to the ImageView
as well. 也将
OnTouchListener
设置为ImageView
。 When you pull away the touch from view's touchable region it will fire a Touch event. 当您从视图的可触摸区域拉开触摸时,它将触发一个Touch事件。 OnClick wont be fired when the ontouch is cancelled(
MotionEvent.ACTION_CANCEL
).You should not consume the onTouch()
therefore return false in onTouch()
当ontouch被取消(的OnClick不会被解雇
MotionEvent.ACTION_CANCEL
)。你不应该消耗onTouch()
因此返回false onTouch()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.