FrameLayout内嵌SurfaceView点击监听器不生效,无法切换视频画面求助
问题原因分析
你的点击事件无法触发主要由三个常见问题导致:
- 你给SurfaceView设置的
OnTouchListener返回了true,触摸事件被SurfaceView直接消费,不会向上传递给父布局mLayoutSmallView - 调用了
surface.setZOrderOnTop(true),会让SurfaceView的渲染层提到Window最顶层,脱离普通View的事件分发体系,导致父布局无法接收点击事件 - 若使用XML配置的
android:onClick="frameclick",需要确保Activity中存在对应签名的方法,否则会触发异常导致点击无响应
解决方案
按以下步骤逐一调整即可解决点击问题:
- 去掉SurfaceView的触摸、点击监听器,或者将
OnTouchListener的返回值改为false,让触摸事件可以向上传递给父布局 - 删除
surface.setZOrderOnTop(true)和surface.setZOrderMediaOverlay(true)的调用,改为给mLayoutSmallView设置elevation控制层级,不会影响事件分发:<FrameLayout android:id="@+id/videoChat_localVideoRender" android:layout_marginBottom="130dp" android:layout_marginRight="20dp" android:layout_width="100dp" android:visibility="visible" android:layout_height="150dp" android:layout_gravity="bottom|right" android:elevation="10dp"/> <!-- 数值比底层VideoGridContainer高即可 --> - 直接在Java代码中给
mLayoutSmallView设置点击监听器,优先级高于XML配置,也更容易排查问题:mLayoutSmallView = findViewById(R.id.videoChat_localVideoRender); mLayoutSmallView.setOnClickListener(v -> { swapVideoSurface(); // 执行大小窗交换逻辑 });
大小窗交换逻辑示例
点击小窗后执行以下代码即可交换两个SurfaceView的展示:
private void swapVideoSurface() { // 取出小窗内的SurfaceView SurfaceView smallSurface = (SurfaceView) mLayoutSmallView.getChildAt(0); // 取出大窗内的SurfaceView,根据你实际的存储逻辑调整取值方式 ViewGroup bigVideoWrapper = (ViewGroup) mVideoLayout.getChildAt(0); SurfaceView bigSurface = (SurfaceView) bigVideoWrapper.getChildAt(0); // 从原有父布局移除 mLayoutSmallView.removeAllViews(); bigVideoWrapper.removeAllViews(); // 互相添加到对方父布局 bigVideoWrapper.addView(smallSurface, new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT )); mLayoutSmallView.addView(bigSurface, new FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT )); // 按需重新给RTC引擎设置渲染画布即可 }
内容的提问来源于stack exchange,提问作者Mamta Rana
相关产品推荐
相关产品推荐

