我正在尝试在 Android 应用程序中创建一个自定义对话框,该对话框使用 RecyclerView 显示列表。但是,当我运行该应用程序时,该对话框仅显示列表的第一项。我想显示列表中的所有项目。
如何显示所有列表
这是我的 MainActivity 、CustomDialog 和 MainDropdownAdapter 类的相关代码
class MainActivity : AppCompatActivity() {
private lateinit var mainDropdownGroup: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
mainDropdownGroup = findViewById(R.id.main_dropdown_group)
val groups = arrayListOf(
MainDropDownModel("Group A", " "),
MainDropDownModel("Group B", "admin"),
MainDropDownModel("Group C", " "),
MainDropDownModel("Group D", " ")
)
mainDropdownGroup.setOnClickListener {
CustomDialog(this, groups) { selectedItem ->
mainDropdownGroup.text = selectedItem.device_title
}.show()
}
}
}
class CustomDialog(
context: Context,
private val dataList: ArrayList<MainDropDownModel>,
private val onItemSelected: (MainDropDownModel) -> Unit
) : Dialog(context) {
private lateinit var recyclerView: RecyclerView
private lateinit var dropdownAdapter: MainDropdownAdapter
init {
val view = LayoutInflater.from(context).inflate(R.layout.item_dropdown_list, null)
setContentView(view)
recyclerView = view.findViewById(R.id.dialog_RV)
recyclerView.layoutManager = LinearLayoutManager(context)
window?.setBackgroundDrawableResource(android.R.color.transparent)
dropdownAdapter = MainDropdownAdapter(context, dataList)
recyclerView.adapter = dropdownAdapter
dropdownAdapter.setOnItemClickListener { item ->
onItemSelected(item)
dismiss()
}
val params = window?.attributes
params?.width = context.resources.displayMetrics.widthPixels - 30.dpToPx(context)
params?.height = WindowManager.LayoutParams.WRAP_CONTENT
window?.attributes = params
}
fun Int.dpToPx(context: Context): Int {
return (this * context.resources.displayMetrics.density).toInt()
}
}
class MainDropdownAdapter(
private val context: Context,
private val dropdownList: ArrayList<MainDropDownModel>
) : RecyclerView.Adapter<MainDropdownAdapter.ViewHolder>() {
private var onItemClickListener: ((MainDropDownModel) -> Unit)? = null
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val itemName: TextView = itemView.findViewById(R.id.dropdown_item_text)
val itemImg: TextView = itemView.findViewById(R.id.dropdown_item_img)
fun bindItems(item: MainDropDownModel) {
itemName.text = item.device_title
itemImg.text = item.device_choice
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_dropdown, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = dropdownList[position]
holder.bindItems(currentItem)
holder.itemView.setOnClickListener {
onItemClickListener?.invoke(currentItem)
}
}
override fun getItemCount(): Int {
return dropdownList.size
}
fun setOnItemClickListener(listener: (MainDropDownModel) -> Unit) {
onItemClickListener = listener
}
}
当我单击 mainDropdownGroup TextView 时,会出现该对话框,但它仅显示组列表中的第一项。我希望看到对话框中列出的所有项目,但只显示第一个。
尝试将item_dropdown_list主布局高度match_parent更改为wrap_content。它将占据 RecyclerView 的整个高度。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- item views -->
</LinearLayout>