elp*_*cko 3 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>
Run Code Online (Sandbox Code Playgroud)
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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
主要活动:
@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);
}
Run Code Online (Sandbox Code Playgroud)
BottomSheetBehavior的实现不支持内部的两个可滚动视图,这就是为什么这种布局永远不会"开箱即用"的原因.但是,这个问题有一个hacky但很简单的解决方法.首先,我们必须通过将该类的代码复制到我们的新CustomBottomSheetBehavior类来自定义BottomSheetBehavior.然后,通过替换行来修改"onLayoutChild"方法
mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
Run Code Online (Sandbox Code Playgroud)
同
if (mNestedScrollingChildRef == null) {
mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
}
Run Code Online (Sandbox Code Playgroud)
mNestedScrollingChildRef在BottomSheetBehavior类中具有包级访问权限,因此我们无法扩展它.
比,添加以下方法:
public void setNestedScrollingChildRef(View v) {
this.mNestedScrollingChildRef = new WeakReference<View>(v);
}
Run Code Online (Sandbox Code Playgroud)
在您的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);
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我们在这里做的是捕获所有触摸事件,来到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);
}
Run Code Online (Sandbox Code Playgroud)
并在您的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;
}
Run Code Online (Sandbox Code Playgroud)
其余代码保持不变.
小智 6
我有同样的问题,要解决这个问题,而无需覆盖 BottomSheetBehavior 或需要额外的库,您可以执行以下操作:在底部工作表实现中实现一个回调,以注册页面的更改。
fun onPageChanged(currentPage: Int) {
nestedScrollView1.isNestedScrollingEnabled = currentPage == 0
nestedScrollView2.isNestedScrollingEnabled = currentPage == 1
...
dialog?.findViewById<View>(R.id.design_bottom_sheet)?.requestLayout()
}
Run Code Online (Sandbox Code Playgroud)
在 onLayoutChild 中的 BottomSheetBehavior 实现中,执行了对支持嵌套滚动的第一个子项的查找,通过此更改重复查找。不是最佳解决方案,但在我的情况下工作正常
小智 6
您在BottomSheetDialogFragment(第一个-水平,第二个-垂直)和第二个不可滚动的recyclerView中有两个recyclerViews吗?设置适配器后,只需将其添加到第一个回收器:
ViewCompat.setNestedScrollingEnabled(recyclerView1, false)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2617 次 |
| 最近记录: |