aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-06-26 15:57:30 -0700
committerAllan Wang <me@allanwang.ca>2017-06-26 15:57:30 -0700
commitf1ad240716d9cb99dd3077a4dc189d67e7803f9f (patch)
treea542cba268e19a3ff23cbddde9267a83d8fbd97a
parent600524d548105aa026c0d1900d503bab34c38ce4 (diff)
downloadkau-f1ad240716d9cb99dd3077a4dc189d67e7803f9f.tar.gz
kau-f1ad240716d9cb99dd3077a4dc189d67e7803f9f.tar.bz2
kau-f1ad240716d9cb99dd3077a4dc189d67e7803f9f.zip
Bind search to root view
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt19
-rw-r--r--library/src/main/res/layout/kau_search_view.xml3
-rw-r--r--sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt2
3 files changed, 14 insertions, 10 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
index d7ff634..423fbcd 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt
@@ -1,5 +1,6 @@
package ca.allanwang.kau.searchview
+import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
@@ -163,7 +164,6 @@ class SearchView @JvmOverloads constructor(
private val divider: View by bindView(R.id.search_divider)
private val recycler: RecyclerView by bindView(R.id.search_recycler)
val adapter = FastItemAdapter<SearchItem>()
- lateinit var parent: ViewGroup
var menuItem: MenuItem? = null
val isOpen: Boolean
get() = card.isVisible()
@@ -228,10 +228,9 @@ class SearchView @JvmOverloads constructor(
configs.config()
}
- fun bind(parent: ViewGroup, menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView {
+ fun bind(activity: Activity, menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView {
config(config)
configs.textObserver(textEvents.filter { it.isNotBlank() }, this)
- this.parent = parent
menuItem = menu.findItem(id)
if (menuItem!!.icon == null) menuItem!!.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor)
card.gone()
@@ -241,12 +240,12 @@ class SearchView @JvmOverloads constructor(
}
fun unBind(replacementMenuItemClickListener: MenuItem.OnMenuItemClickListener? = null) {
- parent.removeView(this)
+ (parent as ViewGroup).removeView(this)
menuItem?.setOnMenuItemClickListener(replacementMenuItemClickListener)
}
fun configureCoords(item: MenuItem) {
- val view = parent.findViewById<View>(item.itemId) ?: return
+ val view = (parent as ViewGroup).findViewById<View>(item.itemId) ?: return
val locations = IntArray(2)
view.getLocationOnScreen(locations)
menuX = (locations[0] + view.width / 2)
@@ -331,10 +330,14 @@ class SearchView @JvmOverloads constructor(
}
}
-fun ViewGroup.bindSearchView(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: SearchView.Configs.() -> Unit = {}): SearchView {
- val searchView = SearchView(context)
+@DslMarker
+annotation class KauSearch
+
+@KauSearch
+fun Activity.bindSearchView(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: SearchView.Configs.() -> Unit = {}): SearchView {
+ val searchView = SearchView(this)
searchView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
- addView(searchView)
+ findViewById<ViewGroup>(android.R.id.content).addView(searchView)
searchView.bind(this, menu, id, menuIconColor, config)
return searchView
}
diff --git a/library/src/main/res/layout/kau_search_view.xml b/library/src/main/res/layout/kau_search_view.xml
index d927c68..8b07403 100644
--- a/library/src/main/res/layout/kau_search_view.xml
+++ b/library/src/main/res/layout/kau_search_view.xml
@@ -101,7 +101,8 @@
android:id="@+id/search_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:overScrollMode="never" />
+ android:overScrollMode="never"
+ android:visibility="gone" />
</LinearLayout>
diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
index 83523fa..46f4cd2 100644
--- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
+++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt
@@ -131,7 +131,7 @@ class MainActivity : KPrefActivity() {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
- searchView = container.bindSearchView(menu, R.id.action_search) {
+ searchView = bindSearchView(menu, R.id.action_search) {
textObserver = {
observable, searchView ->
observable.subscribe {