BottomSheetBehavior带有两个RecyclerView



我在一个带有BottomSheetBehavior的LinearLayout中得到了两个RecyclerView。当您单击第一个RecyclerView(带网格)内的项目时,RecyclerView将设置为Gone,并显示第二个RecycerView(带列表)。当显示第二个回收器时,您不能上下滑动BottomSheet,相反,即使在展开状态下,列表也会滚动。如果第一个回收器启动了,一切都很好。有没有办法让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);
}
});
}

主要活动:

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

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

ViewCompat.setNestedScrollingEnabled(recyclerView1, false)

BottomSheetBehavior的实现不支持内部的两个可滚动视图,这就是为什么这种布局永远不会"开箱即用"。然而,对于这个问题,有一个简单而巧妙的解决方法。首先,我们必须通过将该类的代码复制到新的CustomBottomSheetBehavior类来创建自定义BottomSheetPhavior。然后,通过替换行来修改"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);
}

在你的活动课上:

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中使用此方法需要进一步的复制粘贴。我们应该创建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);
}

在您的片段类中,覆盖"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;
}

代码的其余部分保持不变。

我也遇到了同样的问题,为了在不需要重写BottomSheetBehavior或需要额外库的情况下解决这个问题,您可以执行以下操作:在您的底部工作表实现中实现一个回调,以注册页面的更改。

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

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

最新更新