@@ -6,16 +6,37 @@ import android.view.LayoutInflater
6
6
import android.view.View
7
7
import android.view.ViewGroup
8
8
import androidx.annotation.StyleRes
9
+ import androidx.appcompat.widget.SearchView
9
10
import androidx.fragment.app.Fragment
10
11
import androidx.fragment.app.FragmentActivity
11
12
import androidx.fragment.app.FragmentManager
13
+ import com.google.android.material.bottomsheet.BottomSheetBehavior
14
+ import com.google.android.material.bottomsheet.BottomSheetDialog
12
15
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
13
16
import kotlinx.android.synthetic.main.dialog_sheet_selection.*
14
17
15
18
class SheetSelection private constructor() : BottomSheetDialogFragment() {
16
19
17
20
var onItemClickListener: OnItemSelectedListener ? = null
18
21
22
+ private val adapter by lazy {
23
+ SheetSelectionAdapter (
24
+ source = arguments?.getParcelableArrayList(ARGS_ITEMS ) ? : emptyList(),
25
+ selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION , NO_SELECT ) ? : NO_SELECT ,
26
+ onItemSelectedListener = onItemSelectedListener
27
+ )
28
+ }
29
+
30
+ private val screenHeight by lazy {
31
+ val statusBarHeight = try {
32
+ val resourceId = resources.getIdentifier(" status_bar_height" , " dimen" , " android" )
33
+ resources.getDimensionPixelSize(resourceId)
34
+ } catch (e: Exception ) {
35
+ 0
36
+ }
37
+ resources.displayMetrics.heightPixels - statusBarHeight
38
+ }
39
+
19
40
override fun getTheme (): Int = arguments?.getInt(ARGS_THEME ) ? : super .getTheme()
20
41
21
42
override fun onCreateView (
@@ -36,23 +57,61 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
36
57
val title = args.getString(ARGS_TITLE )
37
58
if (title.isNullOrEmpty()) {
38
59
textViewTitle.visibility = View .GONE
60
+ textViewTitle.text = null
39
61
} else {
62
+ textViewTitle.visibility = View .VISIBLE
40
63
textViewTitle.text = title
41
64
}
42
65
43
- recyclerViewSelectionItems.adapter = SheetSelectionAdapter (
44
- items = args.getParcelableArrayList(ARGS_ITEMS ) ? : emptyList(),
45
- selectedPosition = args.getInt(ARGS_SELECTED_POSITION , NO_SELECT ),
46
- onItemSelectedListener = internalOnItemSelectedListener
47
- )
66
+ if (args.getBoolean(ARGS_SEARCH_ENABLED )) {
67
+ buttonSearch.visibility = View .VISIBLE
68
+ buttonSearch.setOnClickListener(onSearchClickListener)
69
+ searchView.setOnCloseListener(onSearchCloseListener)
70
+ searchView.setOnQueryTextListener(onSearchQueryTextListener)
71
+ }
72
+
73
+ recyclerViewSelectionItems.setHasFixedSize(true )
74
+ recyclerViewSelectionItems.adapter = adapter
48
75
}
49
76
}
50
77
51
- private val internalOnItemSelectedListener: OnItemSelectedListener = { item, position ->
78
+ private fun updateSheetHeight (viewHeight : Int ) {
79
+ rootLayout.layoutParams = rootLayout.layoutParams
80
+ .apply { height = viewHeight }
81
+ }
82
+
83
+ private val onItemSelectedListener: OnItemSelectedListener = { item, position ->
52
84
dismiss()
53
85
onItemClickListener?.invoke(item, position)
54
86
}
55
87
88
+ private val onSearchClickListener = View .OnClickListener {
89
+ (dialog as ? BottomSheetDialog )?.run {
90
+ behavior.state = BottomSheetBehavior .STATE_EXPANDED
91
+ }
92
+ updateSheetHeight(screenHeight)
93
+ viewSwitcherHeader.displayedChild = 1
94
+ searchView.isIconified = false
95
+ }
96
+
97
+ private val onSearchCloseListener = SearchView .OnCloseListener {
98
+ updateSheetHeight(ViewGroup .LayoutParams .WRAP_CONTENT )
99
+ viewSwitcherHeader.displayedChild = 0
100
+ true
101
+ }
102
+
103
+ private val onSearchQueryTextListener = object : SearchView .OnQueryTextListener {
104
+ override fun onQueryTextChange (newText : String? ): Boolean {
105
+ adapter.search(newText)
106
+ return true
107
+ }
108
+
109
+ override fun onQueryTextSubmit (query : String? ): Boolean {
110
+ adapter.search(query)
111
+ return true
112
+ }
113
+ }
114
+
56
115
class Builder (context : Context ) {
57
116
private val manager: FragmentManager ? = when (context) {
58
117
is FragmentActivity -> context.supportFragmentManager
@@ -66,6 +125,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
66
125
private var items: List <SheetSelectionItem > = emptyList()
67
126
private var selectedPosition: Int = NO_SELECT
68
127
private var showDraggedIndicator: Boolean = false
128
+ private var searchEnabled: Boolean = false
69
129
private var listener: OnItemSelectedListener ? = null
70
130
71
131
fun theme (@StyleRes themeId : Int ) = apply {
@@ -93,6 +153,10 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
93
153
this .showDraggedIndicator = show
94
154
}
95
155
156
+ fun searchEnabled (enabled : Boolean ) = apply {
157
+ this .searchEnabled = enabled
158
+ }
159
+
96
160
fun onItemClickListener (listener : OnItemSelectedListener ) = apply {
97
161
this .listener = listener
98
162
}
@@ -105,6 +169,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
105
169
putParcelableArrayList(ARGS_ITEMS , ArrayList (items))
106
170
putInt(ARGS_SELECTED_POSITION , selectedPosition)
107
171
putBoolean(ARGS_SHOW_DRAGGED_INDICATOR , showDraggedIndicator)
172
+ putBoolean(ARGS_SEARCH_ENABLED , searchEnabled)
108
173
}
109
174
onItemClickListener = listener
110
175
}
@@ -124,5 +189,6 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
124
189
private const val ARGS_ITEMS = " SheetSelection:ARGS_ITEMS"
125
190
private const val ARGS_SELECTED_POSITION = " SheetSelection:ARGS_SELECTED_POSITION"
126
191
private const val ARGS_SHOW_DRAGGED_INDICATOR = " SheetSelection:ARGS_SHOW_DRAGGED_INDICATOR"
192
+ private const val ARGS_SEARCH_ENABLED = " SheetSelection:ARGS_SEARCH_ENABLED"
127
193
}
128
194
}
0 commit comments