如何显示底部对话框

3
我希望实现当用户点击按钮 showBottomDialogBtn 时显示对话框。
我还想实现单项选择的 recyclerView。
您可以在这个链接中查看图片:image link

使用 _BottomSheetDialog_。 - Piyush
你能教我怎么做吗?我是安卓新手。@Piyush - Noob
请查看以下链接以了解有关Android BottomSheetDialog的更多信息:https://medium.com/@anitas3791/android-bottomsheetdialog-3871a6e9d538 或 https://medium.com/glucosio-project/moving-from-dialogs-to-bottomsheetdialogs-on-android-15fb8d140295 - ritesh4302
2个回答

0

查看Android底部对话框, https://droidbyme.medium.com/android-bottom-sheet-7e9cfcec6427

小例子:

BottomSheetDialogTest.kt

// 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
    }
}

dialog_test.xml

<?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")
            }
    }

对我的英语不好,抱歉!:)


0

您可以通过创建一个包含BottomSheetBehaviorRecyclerViewCoordinatorLayout来实现此功能。

请参考这个指南获取底部工作表的工作示例,对于内部的可滚动列表视图,这个答案看起来不错(Java)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接