Android-如果视图中有click listener,如何检测触摸监听器

Android-如果视图中有click listener,如何检测触摸监听器,android,android-layout,ontouchlistener,Android,Android Layout,Ontouchlistener,我有线性布局、滚动视图和图像视图的基本布局 ScrollView已注册onTouchListener和imageview onClicklistener。 如果我点击ImageView并退出ImageView,我不会看到带有“onclick”的日志 如果我从imageView(在scrollView上)中点击,并且我正在某处拉动,我会看到带有“on touch”的日志。 当我退出imageView时,如何在imageView上捕捉到该事件 代码如下: ImageView testButton =

我有线性布局、滚动视图和图像视图的基本布局

ScrollView已注册onTouchListener和imageview onClicklistener。 如果我点击ImageView并退出ImageView,我不会看到带有“onclick”的日志

如果我从imageView(在scrollView上)中点击,并且我正在某处拉动,我会看到带有“on touch”的日志。 当我退出imageView时,如何在imageView上捕捉到该事件

代码如下:

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;
    }
});
<?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>
下面是xml:

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

将此设置为您的滚动视图:

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

然后将触发
imageView
onclick
侦听器。这段代码还可能对功能的其他部分产生重大影响。因此,一旦您执行此操作,请检查视图,如果出现任何屏幕跳转,请进行还原。

也将
OnTouchListener
设置为
图像视图。当您将触摸从视图的可触摸区域拉开时,它将触发触摸事件。ontouch被取消时不会触发OnClick(
MotionEvent.ACTION\u CANCEL
)。您不应该使用
ontouch()
,因此在
ontouch()中返回false