查看Android底部对话框, https://droidbyme.medium.com/android-bottom-sheet-7e9cfcec6427。
小例子:
// imports...
class BottomSheetDialogTest : BottomSheetDialogFragment() {
private var _binding: DialogTestBinding? = null
private val binding get() = _binding!!
var _data: ArrayList<SomeDataClass> = arrayListOf()
private val adapter by lazy { SomeAdapter() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = DialogTestBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
loadViews()
loadData()
}
private fun loadData() {
adapterAll.submitList(_data)
}
private fun loadViews() {
binding.apply {
listAll.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
listAll.adapter = adapterAll
adapter.setOnItemClickListener {
//your click actions
}
}
}
override fun onDestroyView() {
super.onDestroyView()
binding.listAll.adapter = null
_binding = null
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bottom_sheet_dialog_bg"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_all"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
然后在你的Fragment或Activity中像这样使用它:
//some code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//init your ui
btnShowDialog.setOnClickListener {
val dialog = HomeSelectSortTypeDialog()
//...
dialog.show(childFragmentManager, "HomeSelectSortTypeDialog 1")
}
}
对我的英语不好,抱歉!:)