使用全屏查看寻呼机时底部 Sheet 拖动问题

Bottom Sheet dragging issue when View Pager with full screen is used

我正在使用带有底部底部全屏图像视图的 View Pager sheet 并且由于向下拖动底部 sheet 不起作用,删除视图寻呼机使其再次工作但是这不是我想要的

我尝试覆盖 ViewPager class 并手动拦截触摸事件,但它也不起作用,并且它以某种方式禁用了向上拖动甚至展开

public class CustomViewPager extends ViewPager {
    public CustomViewPager(@NonNull Context context) {
        super(context);
    }
    
    public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_UP) {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        else {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return true;
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN || ev.getAction() == MotionEvent.ACTION_UP) {
            getParent().requestDisallowInterceptTouchEvent(false);
        }
        else {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return true;
    }
}

底部 Sheet 包含两个框架布局的布局,其中一个是包含 viewpager 的片段容器

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/design_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:background="?attr/nowPlayerBackground"
    android:clickable="true"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:elevation="10dp"
    android:focusable="true"
    android:orientation="horizontal"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <include layout="@layout/now_player_mini" />

    <!-- This is a container of fragments containing view pager -->
    <FrameLayout
        android:id="@+id/now_player_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0" />

</FrameLayout>

包含视图分页器的片段布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:nestedScrollingEnabled="true"
    android:orientation="vertical">


    <!-- This is where the problem is happening -->
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/album_art_slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true" />

    <TextView
        android:id="@+id/song_category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fontFamily="@font/light"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="center"
        android:textColor="?attr/fullNowPlayerTextPrimary"
        android:textSize="12pt" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/fg_now_player_gradient" />

<!-- Some other layout like buttons and seekbar goes here -->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

和查看分页器布局,它只包含一个全屏图像视图

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/album_art_pages_iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

已经为 viewpager 指定了这些布尔值

albumArtViewPager.requestDisallowInterceptTouchEvent(true);
albumArtViewPager.setNestedScrollingEnabled(true);

这几乎是 MainActivity

底部 sheet 的所有内容
nowPlaying = findViewById(R.id.now_playing);
FrameLayout frameLayoutBottomSheet = findViewById(R.id.design_bottom_sheet);
bottomSheetBehavior = BottomSheetBehavior.from(frameLayoutBottomSheet);
PEEK_HEIGHT = nowPlaying.getLayoutParams().height;
bottomSheetBehavior.setPeekHeight(PEEK_HEIGHT, true);

还有我的activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/mainBackground">

    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:translationZ="2dp">

        <include layout="@layout/now_player" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <View
        android:id="@+id/status_bar_background"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_gravity="top"
        android:background="?attr/colorStatusBarBackground" />

</FrameLayout>

这就是问题所在,我希望面板在向下滑动时下降,在向左或向右滑动视图时切换页面

GIF

我找到了问题的根源,是 View 布局引起的,设置 clickablefocusable false 修复了问题

    <!-- The problem was here because the View is intercepting all the touch events and preventing the bottom sheet from going down-->
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        <!-- Setting clickable and focusable to false fixes the problem -->
        android:clickable="false"
        android:focusable="false"

        android:background="@drawable/fg_now_player_gradient" />