【问题标题】:BottomSheetBehavior with two RecyclerView带有两个 RecyclerView 的 BottomSheetBehavior
【发布时间】:2017-02-10 12:31:11
【问题描述】:

我在具有 BottomSheetBehavior 的 LinearLayout 中有两个 RecyclerView。当您单击第一个 RecyclerView(带有网格)内的项目时,RecyclerView 设置为 Gone,并显示第二个 RecyclerView(带有列表)。当显示第二个 Recycler 时,您不能上下滑动 BottomSheet,而是列表即使在展开状态下也会滚动。如果 First Recycler 启动,一切都很好。有没有办法让 BottomSheet 再次上下滑动?

<LinearLayout
        android:id="@+id/sliding_layout_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical"
        app:behavior_hideable="false"
        app:behavior_peekHeight="400dp"
        app:layout_behavior="@string/bottomSheetBehavior">

        <android.support.v7.widget.RecyclerView 
           android:id="@+id/grid"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_marginEnd="@dimen/activity_horizontal_margin"
           android:layout_marginStart="@dimen/activity_horizontal_margin"
           android:background="@color/white"
           android:clickable="true"
           android:scrollbars="none" />

        <android.support.v7.widget.RecyclerView 
           android:id="@+id/list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_marginEnd="@dimen/activity_horizontal_margin"
           android:layout_marginStart="@dimen/activity_horizontal_margin"
           android:background="@color/white"
           android:clickable="true"
           android:scrollbars="none" />
</LinearLayout>

网格适配器:

   @Override
   public void onBindViewHolder(ViewHolder holder, int position) {

    String categorieName = mCategories.get(position);
    final CategoryFilterEvent event = new   CategoryFilterEvent(categorieName);
    holder.grid_item_label.setText(categorieName);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EventBus.getDefault().post(event);
        }
    });
}

主活动:

 @Override
 public void onCreate(Bundle savedInstanceState) {

    linearLayoutManager = new LinearLayoutManager(this);
    listAdapter = new ListAdapter(this, mList);
    recyList.setAdapter(listAdapter);
    recyList.setLayoutManager(linearLayoutManager);

    gridLayoutManager = new GridLayoutManager(this, 3);
    gridAdapter = new GridAdapter(this, new ArrayList<String>());
    recyGrid.setAdapter(gridAdapter);
    recyGrid.setLayoutManager(gridLayoutManager);
}

public void onEventMainThread(CategoryFilterEvent event) {
    recyGrid.setVisibilty(GONE);
    recyList.setVisiblity(VISIBLE);
}

【问题讨论】:

  • 我认为问题在于,当您在其上滑动手指时,底部工作表会滑动(向上和向下),但是当回收站视图进入底部工作表时,甩动(通过手指滑动)动作得到被回收者视图消耗,我认为如果你将回收者视图放在底部表中,那是你的问题。
  • 但它对第一个 RecyclerView 非常有效。
  • 分享一些代码
  • 查看编辑。但我认为实际的代码不会有帮助。
  • 是的,它没有帮助。我需要一些代码和详细信息,这些信息与您如何进入活动、第一个回收站视图在哪里以及第二个回收站视图出现在哪里?

标签: android android-layout android-recyclerview bottom-sheet


【解决方案1】:

BottomSheetBehavior 的实现不支持内部的两个可滚动视图,这就是为什么这种布局永远不会“开箱即用”的原因。但是,有一个 hacky 但简单的解决方法来解决这个问题。首先,我们必须通过将该类的代码复制到我们的新 CustomBottomSheetBehavior 类来制作自定义的 BottomSheetBehavior。然后,通过替换行来修改“onLayoutChild”方法

mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));

if (mNestedScrollingChildRef == null) {
    mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
}

mNestedScrollingChildRef 在 BottomSheetBehavior 类中具有包级访问权限,因此我们无法对其进行扩展。

然后,添加以下方法:

public void setNestedScrollingChildRef(View v) {
    this.mNestedScrollingChildRef = new WeakReference<View>(v);
}

在你的 Activity 类中:

 RecyclerView recyGrid = (RecyclerView)findViewById(R.id.grid);
 RecyclerView recyList = (RecyclerView)findViewById(R.id.list);
 layout = (LinearLayout)findViewById(R.id.sliding_layout_container);

 recyGrid.addOnItemTouchListener(onItemTouchListener);
 recyList.addOnItemTouchListener(onItemTouchListener);

onItemTouchListener 代码:

RecyclerView.OnItemTouchListener onItemTouchListener = new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        setScrollable(layout, rv);
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
};

private void setScrollable(View bottomSheet, RecyclerView recyclerView){
    ViewGroup.LayoutParams params = bottomSheet.getLayoutParams();
    if (params instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coordinatorLayoutParams = (CoordinatorLayout.LayoutParams) params;
        CoordinatorLayout.Behavior behavior = coordinatorLayoutParams.getBehavior();
        if (behavior != null && behavior instanceof CustomBottomSheetBehavior)
            ((CustomBottomSheetBehavior)behavior).setNestedScrollingChildRef(recyclerView);
    }
}

我们在这里所做的是捕获所有触摸事件,这些事件来自 recyclerViews 并将其作为 mNestedScrollingChildRef 添加到 CustomBottomSheetBehavior 类,以便可以以正确的方式处理所有滚动事件。

2018 年 2 月 27 日更新 将此方法与 BottomSheetDialogFragment 一起使用涉及进一步的复制粘贴。我们应该创建扩展 AppCompatDialog 的 CustomBottomSheetDialog,并将所有来自类 BottomSheetDialog 的代码复制到那里。然后将类变量 mBehavior 更改为我上面描述的 CustomBottomSheetBehavior。

然后,修改“wrapInBottomSheet”方法以设置新的行为:

ViewGroup.LayoutParams bottomSheetParams = bottomSheet.getLayoutParams();
    if (bottomSheetParams instanceof CoordinatorLayout.LayoutParams) {
        mBehavior = new CustomBottomSheetBehavior<>();
        mBehavior.setBottomSheetCallback(mBottomSheetCallback);
        mBehavior.setHideable(mCancelable);
        mBehavior.setPeekHeight(*some value here*);
        ((CoordinatorLayout.LayoutParams) bottomSheetParams).setBehavior(mBehavior);
    }

并在您的片段类中重写“onCreateDialog”方法以在此处使用 CustomBottomSheetDialog:

  @NonNull
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    CustomBottomSheetDialog  dialog = new CustomBottomSheetDialog (getActivity(), R.style.YourDialogTheme);
    dialog.setContentView(R.layout.bottom_sheet_page_fragment);

     RecyclerView recyGrid = (RecyclerView)dialog.findViewById(R.id.grid);
     RecyclerView recyList = (RecyclerView)dialog.findViewById(R.id.list);
     layout = (LinearLayout)dialog.findViewById(R.id.sliding_layout_container);

     recyGrid.addOnItemTouchListener(onItemTouchListener);
     recyList.addOnItemTouchListener(onItemTouchListener);
     return dialog;
    }

其余代码保持不变。

【讨论】:

  • 如何将它用于BottomSheetDialogFragment?
  • @RasoulMiri 我已经编辑了我的答案以解释如何将其与 BottomSheetDialogFragment 一起使用
  • 这不是一个有用的解决方案,因为它缺少很多信息。正如您所写:无法从包外部访问该类。所以它不可能使用你上面写的任何方法。由于还有其他仅包功能,您不能简单地将整个行为类复制并粘贴到另一个文件中。
【解决方案2】:

BottomSheetDialogFragment 中是否有两个 recyclerView(第一个 - 水平,第二个 - 垂直)和第二个不可滚动的 recyclerView?设置适配器后,只需将其添加第一个回收器:

ViewCompat.setNestedScrollingEnabled(recyclerView1, false)

【讨论】:

  • 哇!我只是在没有看到您的答案的情况下实施了这个解决方案,现在我能够自己想出这种方法,我感到非常高兴!
  • 谢谢!谁能解释为什么这有效?此解决方案还可以通过在 xml 中的第一个 RecyclerView 上分配 android:nestedScrollingEnabled="false" 来工作。
【解决方案3】:

我遇到了同样的问题,要解决此问题而无需覆盖 BottomSheetBehavior 或需要额外的库,您可以执行以下操作:在底部工作表实现中实现一个回调,以注册页面的更改。

fun onPageChanged(currentPage: Int) {
   nestedScrollView1.isNestedScrollingEnabled = currentPage == 0
   nestedScrollView2.isNestedScrollingEnabled = currentPage == 1
   ...
   dialog?.findViewById<View>(R.id.design_bottom_sheet)?.requestLayout()
}

在 onLayoutChild 的 BottomSheetBehavior 实现中,会查找第一个支持嵌套滚动的子项,并通过此更改重复查找。不是最佳解决方案,但在我的情况下工作正常

【讨论】:

  • 完美答案,这正是我所需要的,谢谢,分辨率非常低。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 2023-02-24
  • 1970-01-01
  • 2016-05-14
  • 2020-08-11
  • 1970-01-01
相关资源
最近更新 更多