public class CultureBottomDialogView {/*** 标题*/private static TextView textTitle;/*** 关闭按钮*/private static TextView textClose;/*** 列表*/private static RecyclerView recyclerView;private static BottomDialogAdapter dialogAdapter;private static List<String> mData = new ArrayList<>();public static BottomSheetDialog showReportDialog(Context context) {/*** Dialog自带白色背景问题,设置style*/int maxHeight = (int) (ScreenUtils.getScreenHeight()*0.7);BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context,R.style.CustomDialog);/*** 高度比较高的自定义bottomsheetdialog弹框在展示时可能会因为大于16:9 比例的关键线而收缩一些高度如何让弹窗全部展示并且收缩的时候全部收缩* 调整peekHeight以避免不必要的收缩:*/bottomSheetDialog.getBehavior().setPeekHeight(maxHeight);View commentView = LayoutInflater.from(context).inflate(R.layout.culture_bottom_dialog_view_layout, null);ConstraintLayout layout = commentView.findViewById(R.id.culture_bottom_dialog_view_parent_layout);layout.setMaxHeight(maxHeight);textTitle = commentView.findViewById(R.id.culture_bottom_dialog_view_title);textClose = commentView.findViewById(R.id.culture_bottom_dialog_view_close);recyclerView = commentView.findViewById(R.id.culture_bottom_dialog_view_recyclerview);recyclerView.setLayoutManager(new LinearLayoutManager(context));dialogAdapter = new BottomDialogAdapter();recyclerView.setAdapter(dialogAdapter);/*** 动态设置RecyclerView的高度不设置外部布局的高度*/dialogAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {@Overridepublic void onChanged() {super.onChanged();//字体大小15sp,上下18padding ,底部边线0.5int itemHeight = SizeUtils.sp2px(15)+SizeUtils.dp2px(36.5f);int recyclerHeight = dialogAdapter.getItemCount() * itemHeight;ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);layoutParams.topToBottom = R.id.culture_bottom_dialog_view_title;if(recyclerHeight>maxHeight){layoutParams.height = maxHeight;}else {layoutParams.height = recyclerHeight;}recyclerView.setLayoutParams(layoutParams);}});dialogAdapter.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {bottomSheetDialog.dismiss();}});textClose.setOnClickListener(new NoDoubleClickListener() {@Overrideprotected void onNoDoubleClick(View v) {bottomSheetDialog.dismiss();}});bottomSheetDialog.setContentView(commentView);/*** 点击外部能否关闭*/bottomSheetDialog.setCancelable(false);bottomSheetDialog.setCanceledOnTouchOutside(false);return bottomSheetDialog;}public static void setRecyclerData(){if(dialogAdapter!=null){for (int i = 0; i < 5; i++) {mData.add("济南");}dialogAdapter.setNewInstance(mData);}}public static void setRecyclerData2(){if(dialogAdapter!=null){for (int i = 0; i < 5; i++) {mData.add("电影");}dialogAdapter.setNewInstance(mData);}}private static class BottomDialogAdapter extends BaseWidgetChildAdapter<String, BaseViewHolder> {public BottomDialogAdapter() {super(R.layout.culture_bottom_dialog_view_recycler_item_layout);}@Overrideprotected void convert(@NonNull BaseViewHolder holder, String s) {TextView textTitle = holder.getView(R.id.culture_bottom_dialog_view_recycler_item_title);textTitle.setText(s);}}}
使用过程中一直有个问题困扰着,设置了高度后往上滑动的时候总有一块伸展的布局
一开始总以为是RecyclerView的问题,后来滑动外层也会伸展确认是外层问题
BottomSheetDialog因为比例问题会有一部分伸展
bottomSheetDialog.getBehavior().setPeekHeight(maxHeight);
直接设置伸展高度是RecyclerView的最大高度
再一个问题就是RecyclerView的条目变化的时候
ConstraintLayout的限制不生效了也就是
layout_constraintTop_toBottomOf不生效了
因为是动态设置RecyclerView的高度,忘了动态添加
layoutParams.topToBottom = R.id.culture_bottom_dialog_view_title;
记住添加