3

I want to implement show dialog when user clicked on a button showBottomDialogBtn

And I also want to implement recyclerView with single item selection

You can check this image in this

image link

Vishal Beep
  • 1,873
  • 1
  • 10
  • 25
Noob
  • 39
  • 3

2 Answers2

0

Checkout bottomsheetdialogs-on-android, https://droidbyme.medium.com/android-bottom-sheet-7e9cfcec6427 .

Small example:

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>

Then use it like that in your fragment or activity:

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

And sorry for my English! :)

0

You can get this by creating a CoordinatorLayout, a layout with the BottomSheetBehavior and a RecyclerView.

Follow this guide to get a working example of the bottom sheet, and for the recycler view inside, this answer seems good (java).

Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39