From 9feeca909d194fabea9bf8d6d08c9bbdfc42fc33 Mon Sep 17 00:00:00 2001 From: Pop Date: Thu, 9 Jul 2020 15:41:10 +0700 Subject: [PATCH 1/3] Fix can selected item when search not found --- .../sheetselection/SheetSelection.kt | 15 ++++- .../sheetselection/SheetSelectionAdapter.kt | 63 +++++++++++++------ .../src/main/res/layout/row_empty_item.xml | 9 +++ 3 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 sheetselection/src/main/res/layout/row_empty_item.xml diff --git a/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelection.kt b/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelection.kt index 9a0c638..3a87f83 100644 --- a/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelection.kt +++ b/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelection.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.annotation.StringRes import androidx.annotation.StyleRes import androidx.appcompat.widget.SearchView import androidx.fragment.app.Fragment @@ -23,6 +24,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { SheetSelectionAdapter( source = arguments?.getParcelableArrayList(ARGS_ITEMS) ?: emptyList(), selectedPosition = arguments?.getInt(ARGS_SELECTED_POSITION, NO_SELECT) ?: NO_SELECT, + searchNotFoundText = arguments?.getString(ARGS_SEARCH_NOT_FOUND_TEXT) ?: "Search not found.", onItemSelectedListener = onItemSelectedListener ) } @@ -112,7 +114,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { } } - class Builder(context: Context) { + class Builder(private val context: Context) { private val manager: FragmentManager? = when (context) { is FragmentActivity -> context.supportFragmentManager is Fragment -> context.requireFragmentManager() @@ -126,6 +128,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { private var selectedPosition: Int = NO_SELECT private var showDraggedIndicator: Boolean = false private var searchEnabled: Boolean = false + private var searchNotFoundText: String? = null private var listener: OnItemSelectedListener? = null fun theme(@StyleRes themeId: Int) = apply { @@ -157,6 +160,14 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { this.searchEnabled = enabled } + fun searchNotFoundText(text: String) = apply { + this.searchNotFoundText = text + } + + fun searchNotFoundText(@StringRes textResId: Int) = apply { + this.searchNotFoundText = context.getString(textResId) + } + fun onItemClickListener(listener: OnItemSelectedListener) = apply { this.listener = listener } @@ -170,6 +181,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { putInt(ARGS_SELECTED_POSITION, selectedPosition) putBoolean(ARGS_SHOW_DRAGGED_INDICATOR, showDraggedIndicator) putBoolean(ARGS_SEARCH_ENABLED, searchEnabled) + putString(ARGS_SEARCH_NOT_FOUND_TEXT, searchNotFoundText) } onItemClickListener = listener } @@ -187,6 +199,7 @@ class SheetSelection private constructor() : BottomSheetDialogFragment() { private const val ARGS_THEME = "SheetSelection:ARGS_THEME" private const val ARGS_TITLE = "SheetSelection:ARGS_TITLE" private const val ARGS_ITEMS = "SheetSelection:ARGS_ITEMS" + private const val ARGS_SEARCH_NOT_FOUND_TEXT = "SheetSelection:ARGS_SEARCH_NOT_FOUND_TEXT" private const val ARGS_SELECTED_POSITION = "SheetSelection:ARGS_SELECTED_POSITION" private const val ARGS_SHOW_DRAGGED_INDICATOR = "SheetSelection:ARGS_SHOW_DRAGGED_INDICATOR" private const val ARGS_SEARCH_ENABLED = "SheetSelection:ARGS_SEARCH_ENABLED" diff --git a/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelectionAdapter.kt b/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelectionAdapter.kt index f654519..a9d2cd0 100644 --- a/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelectionAdapter.kt +++ b/sheetselection/src/main/java/com/minibugdev/sheetselection/SheetSelectionAdapter.kt @@ -12,28 +12,38 @@ typealias OnItemSelectedListener = (item: SheetSelectionItem, position: Int) -> class SheetSelectionAdapter( private val source: List, private val selectedPosition: Int, + private val searchNotFoundText: String, private val onItemSelectedListener: OnItemSelectedListener? -) : RecyclerView.Adapter() { +) : RecyclerView.Adapter() { private var items: List = source override fun getItemCount() = items.size - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - return LayoutInflater.from(parent.context) - .inflate(R.layout.row_selection_item, parent, false) - .run { - ViewHolder(this) - } + override fun getItemViewType(position: Int): Int = + when (items[position].key) { + KEY_SEARCH_NOT_FOUND -> R.layout.row_empty_item + else -> R.layout.row_selection_item + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) + return when (viewType) { + R.layout.row_selection_item -> ItemViewHolder(view) + R.layout.row_empty_item -> EmptyViewHolder(view, searchNotFoundText) + else -> throw IllegalAccessException("Item view type doesn't match.") + } } - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - holder.onBindView( - item = items[position], - position = position, - selected = position == selectedPosition, - onItemSelectedListener = onItemSelectedListener - ) + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + if (holder is ItemViewHolder) { + holder.onBindView( + item = items[position], + position = position, + selected = position == selectedPosition, + onItemSelectedListener = onItemSelectedListener + ) + } } fun search(keyword: String?) { @@ -42,7 +52,11 @@ class SheetSelectionAdapter( } else { val searchResult = source.filter { it.value.contains(keyword, true) } if (searchResult.isEmpty()) { - updateItems(listOf(SheetSelectionItem("search_not_found", "Search not found."))) + updateItems( + listOf( + SheetSelectionItem(KEY_SEARCH_NOT_FOUND, searchNotFoundText) + ) + ) } else { updateItems(searchResult) } @@ -54,11 +68,12 @@ class SheetSelectionAdapter( notifyDataSetChanged() } - class ViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), - LayoutContainer { + class ItemViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView), LayoutContainer { fun onBindView( - item: SheetSelectionItem, position: Int, selected: Boolean, + item: SheetSelectionItem, + position: Int, + selected: Boolean, onItemSelectedListener: OnItemSelectedListener? ) { val selectedIcon = if (selected) R.drawable.ic_check else 0 @@ -70,4 +85,16 @@ class SheetSelectionAdapter( } } } + + class EmptyViewHolder(override val containerView: View, emptyText: String) : RecyclerView.ViewHolder(containerView), + LayoutContainer { + + init { + textViewItem.text = emptyText + } + } + + companion object { + private const val KEY_SEARCH_NOT_FOUND = "SheetSelectionAdapter:search_not_found" + } } \ No newline at end of file diff --git a/sheetselection/src/main/res/layout/row_empty_item.xml b/sheetselection/src/main/res/layout/row_empty_item.xml new file mode 100644 index 0000000..4cbdd19 --- /dev/null +++ b/sheetselection/src/main/res/layout/row_empty_item.xml @@ -0,0 +1,9 @@ + + \ No newline at end of file From c9c25518d8ca8911e16352817079e998ed0b5333 Mon Sep 17 00:00:00 2001 From: Pop Date: Thu, 9 Jul 2020 15:41:48 +0700 Subject: [PATCH 2/3] Update README.md --- README.md | 2 ++ .../java/com/minibugdev/sheetselection/demo/MainActivity.kt | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index d999fbd..6dd4668 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ SheetSelection.Builder(context) .selectedPosition(2) .showDraggedIndicator(true) .searchEnabled(true) + .searchNotFoundText("Nothing!!") .onItemClickListener { item, position -> // DO SOMETHING } @@ -50,6 +51,7 @@ SheetSelection.Builder(context) - Set selected item by `Builder.selectedPosition(Int)`. default is `SheetSelection.NO_SELECT` - Show dragged indicator by `Builder.showDraggedIndicator(Boolean)`. default is `false` - Set search enabled by `Builder.searchEnabled(Boolean)`. default is `false` +- Set search not found text by `Builder.searchNotFoundText(String)`. default is `Search not found.` - Set custom theme by `Builder.theme(@StyleRes)`. - To handle the item click listener by `Builder.onItemClickListener()`. diff --git a/app/src/main/java/com/minibugdev/sheetselection/demo/MainActivity.kt b/app/src/main/java/com/minibugdev/sheetselection/demo/MainActivity.kt index 5dee714..e0ac98a 100644 --- a/app/src/main/java/com/minibugdev/sheetselection/demo/MainActivity.kt +++ b/app/src/main/java/com/minibugdev/sheetselection/demo/MainActivity.kt @@ -50,6 +50,7 @@ class MainActivity : AppCompatActivity() { .selectedPosition(2) .showDraggedIndicator(true) .searchEnabled(true) + .searchNotFoundText("Nothing!!") .theme(R.style.Theme_Custom_SheetSelection) .onItemClickListener { item, position -> textview.text = "You selected `${item.value}`, At position [$position]." From ca458368c557647b79ce7b31f59df87881d84f50 Mon Sep 17 00:00:00 2001 From: Pop Date: Thu, 9 Jul 2020 15:42:57 +0700 Subject: [PATCH 3/3] Pump up version to 0.0.3 --- README.md | 2 +- sheetselection/build.gradle | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6dd4668..ac983b9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ repositories { Add the dependency ``` groovy dependencies { - implementation 'com.github.minibugdev:sheetselection:0.0.2' + implementation 'com.github.minibugdev:sheetselection:0.0.3' } ``` diff --git a/sheetselection/build.gradle b/sheetselection/build.gradle index 8618c33..5a389b8 100644 --- a/sheetselection/build.gradle +++ b/sheetselection/build.gradle @@ -12,8 +12,8 @@ android { defaultConfig { minSdkVersion 17 targetSdkVersion 29 - versionCode 1 - versionName "0.0.2" + versionCode 2 + versionName "0.0.3" } buildTypes {