BottomSheetBehavior有两个RecyclerView

时间:2017-02-10 12:31:11

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

我在LinearLayout中有一个带有BottomSheetBehavior的两个RecyclerView。当您单击第一个RecyclerView内的项目(带网格)时,RecyclerView将设置为Gone,并显示第二个RecyclerView(带有列表)。当显示第二个Recycler时,您无法上下滑动BottomSheet而不是List即使在Expanded State中也在滚动。如果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>

GridAdapter:

   @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);
        }
    });
}

MainActivity:

 @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);
}

3 个答案:

答案 0 :(得分:6)

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类,以便可以正确的方式处理所有滚动事件。

27.02.2018更新 在BottomSheetDialogFragment中使用这种方法涉及进一步的复制粘贴。我们应该创建CustomBottomSheetDialog,扩展AppCompatDialog并复制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);
    }

在你的fragment类中覆盖“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;
    }

其余代码保持不变。

答案 1 :(得分:6)

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

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

在onLayoutChild的BottomSheetBehavior实现中,将执行对第一个支持嵌套滚动的子级的查找,此更改将重复进行。不是最佳解决方案,但在我的情况下效果很好

答案 2 :(得分:2)

您是否在BottomSheetDialogFragment内有两个recyclerViews(第一个–水平,第二个–垂直),而第二个recyclerView无法滚动?设置适配器后,只需将此添加到第一个回收器即可:

ViewCompat.setNestedScrollingEnabled(recyclerView1, false)