Skip to content

Commit d6958f9

Browse files
authored
Merge pull request #6 from minibugdev/bugs/search_not_found
Fix can selected item when search not found
2 parents 0f99a35 + ca45836 commit d6958f9

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repositories {
1818
Add the dependency
1919
``` groovy
2020
dependencies {
21-
implementation 'com.github.minibugdev:sheetselection:0.0.2'
21+
implementation 'com.github.minibugdev:sheetselection:0.0.3'
2222
}
2323
```
2424

@@ -39,6 +39,7 @@ SheetSelection.Builder(context)
3939
.selectedPosition(2)
4040
.showDraggedIndicator(true)
4141
.searchEnabled(true)
42+
.searchNotFoundText("Nothing!!")
4243
.onItemClickListener { item, position ->
4344
// DO SOMETHING
4445
}
@@ -50,6 +51,7 @@ SheetSelection.Builder(context)
5051
- Set selected item by `Builder.selectedPosition(Int)`. default is `SheetSelection.NO_SELECT`
5152
- Show dragged indicator by `Builder.showDraggedIndicator(Boolean)`. default is `false`
5253
- Set search enabled by `Builder.searchEnabled(Boolean)`. default is `false`
54+
- Set search not found text by `Builder.searchNotFoundText(String)`. default is `Search not found.`
5355
- Set custom theme by `Builder.theme(@StyleRes)`.
5456
- To handle the item click listener by `Builder.onItemClickListener()`.
5557

app/src/main/java/com/minibugdev/sheetselection/demo/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class MainActivity : AppCompatActivity() {
5050
.selectedPosition(2)
5151
.showDraggedIndicator(true)
5252
.searchEnabled(true)
53+
.searchNotFoundText("Nothing!!")
5354
.theme(R.style.Theme_Custom_SheetSelection)
5455
.onItemClickListener { item, position ->
5556
textview.text = "You selected `${item.value}`, At position [$position]."

sheetselection/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 17
1414
targetSdkVersion 29
15-
versionCode 1
16-
versionName "0.0.2"
15+
versionCode 2
16+
versionName "0.0.3"
1717
}
1818

1919
buildTypes {

sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelection.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import androidx.annotation.StringRes
89
import androidx.annotation.StyleRes
910
import androidx.appcompat.widget.SearchView
1011
import androidx.fragment.app.Fragment
@@ -23,6 +24,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
2324
SheetSelectionAdapter(
2425
source = arguments?.getParcelableArrayList(ARGS_ITEMS) ?: emptyList(),
2526
selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION, NO_SELECT) ?: NO_SELECT,
27+
searchNotFoundText = arguments?.getString(ARGS_SEARCH_NOT_FOUND_TEXT) ?: "Search not found.",
2628
onItemSelectedListener = onItemSelectedListener
2729
)
2830
}
@@ -112,7 +114,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
112114
}
113115
}
114116

115-
class Builder(context: Context) {
117+
class Builder(private val context: Context) {
116118
private val manager: FragmentManager? = when (context) {
117119
is FragmentActivity -> context.supportFragmentManager
118120
is Fragment -> context.requireFragmentManager()
@@ -126,6 +128,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
126128
private var selectedPosition: Int = NO_SELECT
127129
private var showDraggedIndicator: Boolean = false
128130
private var searchEnabled: Boolean = false
131+
private var searchNotFoundText: String? = null
129132
private var listener: OnItemSelectedListener? = null
130133

131134
fun theme(@StyleRes themeId: Int) = apply {
@@ -157,6 +160,14 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
157160
this.searchEnabled = enabled
158161
}
159162

163+
fun searchNotFoundText(text: String) = apply {
164+
this.searchNotFoundText = text
165+
}
166+
167+
fun searchNotFoundText(@StringRes textResId: Int) = apply {
168+
this.searchNotFoundText = context.getString(textResId)
169+
}
170+
160171
fun onItemClickListener(listener: OnItemSelectedListener) = apply {
161172
this.listener = listener
162173
}
@@ -170,6 +181,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
170181
putInt(ARGS_SELECTED_POSITION, selectedPosition)
171182
putBoolean(ARGS_SHOW_DRAGGED_INDICATOR, showDraggedIndicator)
172183
putBoolean(ARGS_SEARCH_ENABLED, searchEnabled)
184+
putString(ARGS_SEARCH_NOT_FOUND_TEXT, searchNotFoundText)
173185
}
174186
onItemClickListener = listener
175187
}
@@ -187,6 +199,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() {
187199
private const val ARGS_THEME = "SheetSelection:ARGS_THEME"
188200
private const val ARGS_TITLE = "SheetSelection:ARGS_TITLE"
189201
private const val ARGS_ITEMS = "SheetSelection:ARGS_ITEMS"
202+
private const val ARGS_SEARCH_NOT_FOUND_TEXT = "SheetSelection:ARGS_SEARCH_NOT_FOUND_TEXT"
190203
private const val ARGS_SELECTED_POSITION = "SheetSelection:ARGS_SELECTED_POSITION"
191204
private const val ARGS_SHOW_DRAGGED_INDICATOR = "SheetSelection:ARGS_SHOW_DRAGGED_INDICATOR"
192205
private const val ARGS_SEARCH_ENABLED = "SheetSelection:ARGS_SEARCH_ENABLED"

sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelectionAdapter.kt

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,38 @@ typealias OnItemSelectedListener = (item: SheetSelectionItem, position: Int) ->
1212
class SheetSelectionAdapter(
1313
private val source: List<SheetSelectionItem>,
1414
private val selectedPosition: Int,
15+
private val searchNotFoundText: String,
1516
private val onItemSelectedListener: OnItemSelectedListener?
16-
) : RecyclerView.Adapter<SheetSelectionAdapter.ViewHolder>() {
17+
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
1718

1819
private var items: List<SheetSelectionItem> = source
1920

2021
override fun getItemCount() = items.size
2122

22-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
23-
return LayoutInflater.from(parent.context)
24-
.inflate(R.layout.row_selection_item, parent, false)
25-
.run {
26-
ViewHolder(this)
27-
}
23+
override fun getItemViewType(position: Int): Int =
24+
when (items[position].key) {
25+
KEY_SEARCH_NOT_FOUND -> R.layout.row_empty_item
26+
else -> R.layout.row_selection_item
27+
}
28+
29+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
30+
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
31+
return when (viewType) {
32+
R.layout.row_selection_item -> ItemViewHolder(view)
33+
R.layout.row_empty_item -> EmptyViewHolder(view, searchNotFoundText)
34+
else -> throw IllegalAccessException("Item view type doesn't match.")
35+
}
2836
}
2937

30-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
31-
holder.onBindView(
32-
item = items[position],
33-
position = position,
34-
selected = position == selectedPosition,
35-
onItemSelectedListener = onItemSelectedListener
36-
)
38+
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
39+
if (holder is ItemViewHolder) {
40+
holder.onBindView(
41+
item = items[position],
42+
position = position,
43+
selected = position == selectedPosition,
44+
onItemSelectedListener = onItemSelectedListener
45+
)
46+
}
3747
}
3848

3949
fun search(keyword: String?) {
@@ -42,7 +52,11 @@ class SheetSelectionAdapter(
4252
} else {
4353
val searchResult = source.filter { it.value.contains(keyword, true) }
4454
if (searchResult.isEmpty()) {
45-
updateItems(listOf(SheetSelectionItem("search_not_found", "Search not found.")))
55+
updateItems(
56+
listOf(
57+
SheetSelectionItem(KEY_SEARCH_NOT_FOUND, searchNotFoundText)
58+
)
59+
)
4660
} else {
4761
updateItems(searchResult)
4862
}
@@ -54,11 +68,12 @@ class SheetSelectionAdapter(
5468
notifyDataSetChanged()
5569
}
5670

57-
class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),
58-
LayoutContainer {
71+
class ItemViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer {
5972

6073
fun onBindView(
61-
item: SheetSelectionItem, position: Int, selected: Boolean,
74+
item: SheetSelectionItem,
75+
position: Int,
76+
selected: Boolean,
6277
onItemSelectedListener: OnItemSelectedListener?
6378
) {
6479
val selectedIcon = if (selected) R.drawable.ic_check else 0
@@ -70,4 +85,16 @@ class SheetSelectionAdapter(
7085
}
7186
}
7287
}
88+
89+
class EmptyViewHolder(override val containerView: View, emptyText: String) : RecyclerView.ViewHolder(containerView),
90+
LayoutContainer {
91+
92+
init {
93+
textViewItem.text = emptyText
94+
}
95+
}
96+
97+
companion object {
98+
private const val KEY_SEARCH_NOT_FOUND = "SheetSelectionAdapter:search_not_found"
99+
}
73100
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/textViewItem"
5+
style="?attr/sheetSelection_itemStyle"
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content"
8+
tools:text="@tools:sample/lorem"
9+
tools:theme="@style/Theme.SheetSelection" />

0 commit comments

Comments
 (0)