+-
![android – BottomSheetBehavior有两个RecyclerView android – BottomSheetBehavior有两个RecyclerView](/img/no_img.png)
我在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);
}
});
}
主要活动:
@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);
}
最佳答案
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;
}
其余代码保持不变.
点击查看更多相关文章
转载注明原文:android – BottomSheetBehavior有两个RecyclerView - 乐贴网