How to prevent ScrollView from catching click / touch events of a view behind it?

I have ScrollView and ImageView inside FrameLayout. ImageView is behind a scroll view

My ScrollView has transparent space (LinearLayout s_layout_transparent with a height of 925 pixels).

So, my ImageView can be seen through this transparent space, but cannot click .

I tried adding a value (android: clickable = "false" android: focusable = "android: focusableInTouchMode =" false ") to scroll the view to prevent ImageView from being intercepted, but this does not work at all.

Here is my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:gravity="top|center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingRight="10dp" > <ImageView android:visibility="visible" android:id="@+id/s_imgv_splash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/temp_detail_screen_splash" android:adjustViewBounds="true" android:scaleType="fitXY"/> </LinearLayout> <com.dreambox.android.saven.Views.ScrollView android:id="@+id/s_scrollview_event_detail" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none" android:visibility="visible" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:focusableInTouchMode="false" android:focusable="false"> <LinearLayout android:id="@+id/s_layout_transparent" android:layout_gravity="center_vertical" android:layout_width="match_parent" android:layout_height="925px" android:orientation="horizontal" android:background="@color/trasparent" android:clickable="false" android:focusableInTouchMode="false" android:focusable="false"> </LinearLayout> <LinearLayout...> <LinearLayout...> </LinearLayout> </com.dreambox.android.saven.Views.ScrollView> </FrameLayout> 
+5
source share
5 answers

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

 dummyView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { // pass on the touch event to the ImageView s_imgv_splash.dispatchTouchEvent(event); // returning true because we handled the event return true; } }); 
+7
source

I solved this problem. Posting this for someone who is facing the same problem now / in the future. Thank you for the offer of Mahesh. Based on Maheshโ€™s assumption, I did so. This is my layout.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: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"> <TextView android:id="@+id/bg_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TestView" android:layout_marginTop="5dip"/> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:alpha="0" /> <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@id+/wrapper" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/transparent" android:orientation="vertical" > <LinearLayout android:id="@+id/dummy" android:layout_width="fill_parent" android:background="@android:color/transparent" android:layout_height="500dip" android:orientation="vertical"/> <LinearLayout android: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 could not click on TextView @ id / bg_tv. The following code solved my problem.

 //dispatch touch events mDummyView = findViewById(R.id.dummy); mDummyView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { mTextView.dispatchTouchEvent(motionEvent); return true; } }); mLayout = (LinearLayout)findViewById(R.id.scroll_); mLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (scrollPercent <= 0.25f) { mTextView.dispatchTouchEvent(motionEvent); return true; } return false; } }); 

In the case of mlayout, TouchEvents are dispatched only after scrollView has moved to the bottom of the screen.

PS: I am writing an answer for the first time. I'm sorry if you think the formatting is very bad or the answer is not written the way it should be.

+4
source

I can not give an exact solution

Check if this can help.

 yourscrollview.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(touch exists inside imageview boundaries) { do imageview click action } return false; } }) 
+1
source

ScrollView is the last in FrameLayout, so 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 must be set to invisible.

0
source

I also used a scroll shift, and it gave a great result on click events.

 blockingViewInScroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { event.offsetLocation(-scrollView.getScrollX(), -scrollView.getScrollY()); blockedViewUnderScroll.dispatchTouchEvent(event); return true; } }); 
0
source

Source: https://habr.com/ru/post/1202563/


All Articles