我有两个 Activity,其中一个显示 RecyclerView,另一个显示图像。单击 RecyclerView 中的一个项目会显示第二个 Activity,其中包含与单击的项目相关的小图像,背景是透明的,显示第一个 Activity。
我试图让第二个 Activity 忽略图像边界之外的所有触摸事件,以便列表可以随着屏幕上的图像滚动。这是可以实现的
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
然而,这仅在第二个 Activity 的窗口未占据整个屏幕时有效。这是第二个 Activity 布局的示例:
<?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>
由于 CustomFrameLayout 的高度和宽度与父级相匹配,它会拦截所有不在 ImageView 内部的触摸事件。
我认为覆盖 CustomFrameLayout 中的 onInterceptTouchEvent 可以让我使用以下代码忽略不需要的触摸事件:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (/* Is touch event within bounds of ImageView */) {
return true;
}
return false; //Otherwise don't intercept
}
但这也不行,有没有人有任何其他想法?
假设 FrameLayout 需要全屏,而我不能使用 Fragments,这可行吗?
最佳答案
你不能那样做。当第二个 Activity 启动时,第一个 Activity 暂停。暂停时,它不会处理任何触摸事件。执行此操作的正确方法是让您的第二个 Activity 成为第一个 Activity 中的 fragment 。
即使您设法找到一种方法来强制它触摸事件,它也不一定会像您希望的那样工作。当调用 onPaused 时,编写良好的 Activity 会停止很多处理。之后它可能无法正确处理触摸,并可能导致崩溃。
关于java - 将触摸事件传递给当前 Activity 后面的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35351979/