Skip to content Skip to sidebar Skip to footer

How To Prevent Scrollview From Intercepting Click/touch Events Of The View Behind It?

I have a ScrollView and an ImageView inside a FrameLayout. The ImageView is behind the scroll view My ScrollView have a transparent space (LinearLayout s_layout_transparent with 92

Solution 1:

if you can fill the transparent parts with an empty view (lets call it dummyView), you can do something like...

dummyView.setOnTouchListener(newView.OnTouchListener() {
    @OverridepublicbooleanonTouch(View view, MotionEvent event) {
        // pass on the touch event to the ImageView
        s_imgv_splash.dispatchTouchEvent(event);
        // returning true because we handled the eventreturntrue;
    }
});

Solution 2:

I solved this issue. Posting this for someone facing the same issue at present/in the future. Thanks for the suggestion Mahesh. Based on the suggestion from Mahesh, this is how i did it. This is my layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/scroll_view"android:background="#ffffff"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/bg_tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TestView"android:layout_marginTop="5dip"/><Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/background_dark"android:alpha="0"
        /><ScrollViewandroid:id="@+id/sv"android:layout_width="fill_parent"android:layout_height="wrap_content"
        ><LinearLayoutandroid:id="@id+/wrapper"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@android:color/transparent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/dummy"android:layout_width="fill_parent"android:background="@android:color/transparent"android:layout_height="500dip"android:orientation="vertical"/><LinearLayoutandroid:id="@+id/scroll_"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" >

             ................
             ................


          </LinearLayout></LinearLayout></ScrollView></RelativeLayout>

After adding LinearLayouts @id/dummy and @id/scroll_ , I was unable to click on TextView @id/bg_tv. The following code solved my problem.

//dispatch touch events
mDummyView = findViewById(R.id.dummy);
mDummyView.setOnTouchListener(newView.OnTouchListener() {
    @OverridepublicbooleanonTouch(View view, MotionEvent motionEvent) {
        mTextView.dispatchTouchEvent(motionEvent);
        returntrue;
    }
});

mLayout = (LinearLayout)findViewById(R.id.scroll_);
mLayout.setOnTouchListener(newView.OnTouchListener() {
    @OverridepublicbooleanonTouch(View view, MotionEvent motionEvent) {
        if (scrollPercent <= 0.25f) {
            mTextView.dispatchTouchEvent(motionEvent);
            returntrue;
        }
        returnfalse;
    }
});

In the case of mLayout, TouchEvents are dispatched only after the scrollView goes to the bottom of the screen.

PS: I am writing an answer for the first time. I am sorry if you find the formatting very poor or if the answer is not written the way it is supposed to be.

Solution 3:

I cant give exact solution

Check it may helps

yourscrollview.setOnTouchListener(newOnTouchListener() {

            @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                if(touch exists inside imageview boundaries)
                {
                    do imageview click action
                }
                returnfalse;
            }
        })

Solution 4:

The ScrollView is last in FrameLayout therefore it has preference.

When your LinearLayout with ImageView is activated, you need to say

findViewById(R.id.s_scrollview_event_detail).setVisibility(View.INVISIBLE)

and vice versa, when ScrollView is activated, the "sibling" Layout view must be set invisible.

Solution 5:

Check to see if the coordinates of the event are within the bounds of the view you want to receive the event. If there is a match, use the performClick method to pass the event along to the desired view. Here is an example:

overridefunonCreate(savedInstanceState: Bundle?) {
    ....
    val viewUnderDummySpace = findViewById<View>(R.id.your_textview_or_button_or_image)
    val dummySpace = scrollView.findViewById<View>(R.id.dummy_space)
    dummySpace.setOnTouchListener(object : View.OnTouchListener {
        overridefunonTouch(p0: View?, event: MotionEvent?): Boolean {
            event?.let {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (inViewInBounds(viewUnderDummySpace, event.rawX.toInt(), event.rawY.toInt())) {
                        //Click performed within bounds of the desired view
                        viewUnderDummySpace.performClick()
                    }
                }
            }

            returntrue
        }
    })
}

privatevar outRect = Rect()
privatevar location = IntArray(2)

privatefuninViewInBounds(view: View, x: Int, y: Int): Boolean {
    view.getDrawingRect(outRect)
    view.getLocationOnScreen(location)
    outRect.offset(location[0], location[1])
    return outRect.contains(x, y)
}

Post a Comment for "How To Prevent Scrollview From Intercepting Click/touch Events Of The View Behind It?"