diff options
Diffstat (limited to 'searchview/src')
-rw-r--r-- | searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt | 2 | ||||
-rw-r--r-- | searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt index dabf4bc..50e0298 100644 --- a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -316,7 +316,7 @@ class SearchView @JvmOverloads constructor( * with the option to replace the item click listener */ fun unBind(replacementMenuItemClickListener: ((item: MenuItem) -> Boolean)? = null) { - parentViewGroup.removeView(this) + (parent as? ViewGroup)?.removeView(this) menuItem?.setOnMenuItemClickListener(replacementMenuItemClickListener) menuItem = null } diff --git a/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt new file mode 100644 index 0000000..3f81dd0 --- /dev/null +++ b/searchview/src/main/kotlin/ca/allanwang/kau/searchview/SearchViewHolder.kt @@ -0,0 +1,25 @@ +package ca.allanwang.kau.searchview + +import android.view.MenuItem + +/** + * Created by Allan Wang on 2017-11-12. + * + * Interface to help facilitate searchview binding and actions + */ +interface SearchViewHolder { + + var searchView: SearchView? + + fun searchViewBindIfNull(binder: () -> SearchView) { + if (searchView == null) searchView = binder() + } + + fun searchViewOnBackPress() = searchView?.onBackPressed() ?: false + + fun searchViewUnBind(replacementMenuItemClickListener: ((item: MenuItem) -> Boolean)? = null) { + searchView?.unBind(replacementMenuItemClickListener) + searchView = null + } + +}
\ No newline at end of file |