Android自定义Dialog中RecyclerView仅显示首个Item问题求助
解决Dialog中RecyclerView仅显示单个Item的问题
修复步骤:
1. 调整Dialog布局的RecyclerView约束
打开res/layout/item_dropdown_list.xml,将RecyclerView的高度设为match_parent,让它填充Dialog的可用空间,同时确保父容器高度为wrap_content:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/dialog_RV" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2. 修正列表Item的布局高度
打开res/layout/item_dropdown.xml,将Item根布局的高度设为wrap_content,避免单个Item占满RecyclerView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="horizontal"> <TextView android:id="@+id/dropdown_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> <TextView android:id="@+id/dropdown_item_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:marginStart="8dp"/> </LinearLayout>
3. 正确Inflate Dialog布局
在CustomDialog的初始化代码中,修改布局加载方式,确保布局参数生效:
将原代码:
val view = LayoutInflater.from(context).inflate(R.layout.item_dropdown_list, null)
替换为:
val parent = window?.decorView?.findViewById(android.R.id.content) as ViewGroup val view = LayoutInflater.from(context).inflate(R.layout.item_dropdown_list, parent, false)
这样布局文件中定义的layout_*属性会被正确解析应用。
4. 确认LayoutManager设置
确保CustomDialog中已正确设置LinearLayoutManager(你的代码已包含此逻辑,无需修改):
recyclerView.layoutManager = LinearLayoutManager(context)
完成以上修改后,Dialog即可正常显示所有列表条目。
内容的提问来源于stack exchange,提问作者onyuuu
相关产品推荐
相关产品推荐

