A quick and easy way to implement a selectable list (single/multiple) via Bottom Sheet Dialog Fragment.
Add it via gradle
dependencies {
// ...
implementation 'com.kishannareshpal:bottomlistsheet:<version>'
// ...
}
/*
List of items you want to show
The items should be:
- Item(text: String, iconRes: Int? = null)
*/
val dataList = listOf<Item>(
Item("Maputo", R.drawable.maputo),
Item("Inhambane", R.drawable.inhambane),
Item("Beira", R.drawable.beira),
Item("Nampula", R.drawable.nampula)
)
BottomListSheet()
.title("Filter")
.subtitle("By specific cities")
.items(dataList)
.onItemSelected { position ->
Log.d(TAG, "You have clicked item at position: $position -> ${data[position]}")
}
.onConfirm { button, selectedItems ->
Log.d(TAG, "You have selected ${selectedItems.size} items!")
}
.preselectItems(listOf(1, 2));
.show(supportFragmentManager) // or if in a fragment, use activity?.supportfragmentManager
// .dismiss() // use to dismiss the sheet at any time
// Available properties:
.isMultipleSelection // to get or set multiple selection mode
.selectedItemsPositions // to get the list of selected items positions
Method | Default value | Description | |
---|---|---|---|
Title | title |
null |
Set the title |
Subtitle | subtitle |
null |
Set the subtitle |
Selectable items | items |
[] |
An array list of items Item(string, int) |
Show as dark mode | isNightMode |
false |
Use this option to show the bottom sheet as dark mode |
On item clicked | onItemSelected |
null |
The block of code to run whenever a single item in the list is tapped. Providing you with the position |
On confirm button clicked | onConfirm |
null |
The block of code to run when the confirm button is tapped. Providing you with the selectedItemsList |
On close button clicked | onClose |
null |
The block of code to run when the close button is tapped. |
Preselected items | preselectedItems |
null |
The list of the items position, which you want to be selected by default when the sheet is set to multiple selection. |
Clear all selected items | clearAllSelection |
n/a |
Use to clear all selected items when the sheet is set to be multiple selected. |
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Copyright 2020 Kishan Jadav
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.