【发布时间】:2016-02-11 23:15:39
【问题描述】:
我有两个活动,其中一个显示 RecyclerView,另一个显示图像。单击 RecyclerView 中的一个项目会显示第二个 Activity,其中包含与单击的项目相关的小图像,并且背景是透明的,显示第一个 Activity。
我试图让第二个 Activity 忽略图像边界之外的所有触摸事件,以便列表可以与屏幕上的图像一起滚动。这可以通过
实现getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
但是,这仅在第二个 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,这样可行吗?
【问题讨论】:
标签: java android android-activity touch