欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > BottomSheetDialog高度自适应,布局RecyclerView使用问题

BottomSheetDialog高度自适应,布局RecyclerView使用问题

2024/11/30 4:58:43 来源:https://blog.csdn.net/csdn_zxw/article/details/139354327  浏览:    关键词:BottomSheetDialog高度自适应,布局RecyclerView使用问题
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;

记住添加

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com