【问题标题】:How to show bottom dialog如何显示底部对话框
【发布时间】:2021-08-23 10:11:40
【问题描述】:

我想在用户点击按钮时实现显示对话框showBottomDialogBtn

而且我还想用单项选择实现recyclerView

您可以在此查看此图片

【问题讨论】:

标签: java android android-recyclerview dialog


【解决方案1】:

结帐bottomsheetdialogs-on-androidhttps://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>

然后在您的片段或活动中使用它:

//some code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      //init your ui
        btnShowDialog.setOnClickListener {
                val dialog = HomeSelectSortTypeDialog()
                //...
                dialog.show(childFragmentManager, "HomeSelectSortTypeDialog 1")
            }
    }

对不起我的英语! :)

【讨论】:

    【解决方案2】:

    您可以通过创建 CoordinatorLayoutBottomSheetBehaviorRecyclerView 的布局来实现此目的。

    按照this guide 获取底部工作表的工作示例,对于内部的回收器视图,this answer 似乎不错 (java)。

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 2018-12-12
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 2018-05-15
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多