Reputation: 3089
I have two Activities, one of them displays a RecyclerView and the other displays an image. Clicking on an item in the RecyclerView displays the second Activity with a small image related to the clicked item and the background being transparent, showing the first Activity.
I'm trying to get the second Activity to ignore all touch events outside the bounds of the image, so that the list can be scrolled with the Image on screen. This is achievable with
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
However this only works when the Window for the second Activity does not take up the whole screen. This is an example of the second Activities layout:
<?xml version="1.0" encoding="utf-8"?>
<CustomFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent">
<ImageView
android:id="@+id/imageView_item"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@color/colorPrimary"
android:layout_gravity="center"/>
</CustomFrameLayout>
Since the CustomFrameLayout has a height and width that matches the parent, it intercepts all touch events that aren't inside the ImageView.
I thought that overriding onInterceptTouchEvent in CustomFrameLayout would allow me to ignore unwanted touch events with the following code:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (/* Is touch event within bounds of ImageView */) {
return true;
}
return false; //Otherwise don't intercept
}
But that doesn't work either, does anyone have any other ideas?
Let's say that the FrameLayout needs to be fullscreen and I can't use Fragments, is this doable?
Upvotes: 1
Views: 2894
Reputation: 93561
You can't do that. When the second Activity is launched, the first Activity is paused. While paused, it will not process any touch events. The correct way to do this is to make your second activity a Fragment in the first activity instead.
Even if you manage to find a way to force it touch events, it wouldn't necessarily work as you hope. A well written Activity stops a lot of its processing when onPaused is called. It may not be possible for it to handle touches correctly afterwards, and attempting to may crash it.
Upvotes: 3