这个问题可能是由于 BottomSheetDialogFragment 的高度设置不正确导致的。当 BottomSheetDialogFragment 中的 RecyclerView 填充了整个屏幕时,它就可能会阻止 BottomSheetDialogFragment 上面的触摸事件。这将导致 RecyclerView 无法滚动或翻页。

要解决这个问题,可以尝试以下几个解决方案:

  1. 将 BottomSheetDialogFragment 的高度设置为适当的大小,以便 RecyclerView 可以在其中滚动。可以使用以下代码来实现:
@Override
public void onStart() {
    super.onStart();
    // 设置 BottomSheetDialogFragment 的高度
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(R.dimen.bottom_sheet_height));
}
  1. 在 RecyclerView 上添加一个触摸事件监听器,以便在滚动 RecyclerView 时关闭 BottomSheetDialogFragment。可以使用以下代码来实现:
recyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            dismiss();
            return true;
        }
        return false;
    }
});

这将在用户滚动 RecyclerView 时关闭 BottomSheetDialogFragment。

  1. 将 RecyclerView 嵌套在 NestedScrollView 中,以便在 RecyclerView 翻页时可以滚动整个 BottomSheetDialogFragment。可以使用以下代码来实现:
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.widget.NestedScrollView>

这将允许 RecyclerView 在翻页时滚动整个 BottomSheetDialogFragment。

BottomSheetDialogFragment 嵌套 recyclerview 导致 recyclerview 无法翻页

原文地址: https://www.cveoy.top/t/topic/bFlJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录