diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-24 20:00:35 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-24 20:00:35 -0700 |
commit | 35b26d771199c1dbf0d67377627c9d8b27da388c (patch) | |
tree | 7b8aa38ef8c0cc43cd5484626c6540bff7798940 | |
parent | 6ad176dde0a84a0eb96dea2f9c7eb34394045526 (diff) | |
download | kau-35b26d771199c1dbf0d67377627c9d8b27da388c.tar.gz kau-35b26d771199c1dbf0d67377627c9d8b27da388c.tar.bz2 kau-35b26d771199c1dbf0d67377627c9d8b27da388c.zip |
Create tintable ripples
10 files changed, 81 insertions, 31 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt index fac5ca1..f8b6ab6 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchItem.kt @@ -3,10 +3,12 @@ package ca.allanwang.kau.searchview import android.support.v7.widget.RecyclerView import android.view.View import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView import ca.allanwang.kau.R import ca.allanwang.kau.utils.bindView import ca.allanwang.kau.utils.setIcon +import ca.allanwang.kau.views.SimpleRippleDrawable import com.mikepenz.fastadapter.items.AbstractItem import com.mikepenz.google_material_typeface_library.GoogleMaterial import com.mikepenz.iconics.typeface.IIcon @@ -22,7 +24,8 @@ class SearchItem(val key: String, val content: String = key, val iicon: IIcon? = ) : AbstractItem<SearchItem, SearchItem.ViewHolder>() { companion object { - var foregroundColor: Int? = null + var foregroundColor: Int = 0xdd000000.toInt() + var backgroundColor: Int = 0xfffafafa.toInt() } override fun getLayoutRes(): Int = R.layout.kau_search_item @@ -33,10 +36,9 @@ class SearchItem(val key: String, val content: String = key, val iicon: IIcon? = override fun bindView(holder: ViewHolder, payloads: MutableList<Any>?) { super.bindView(holder, payloads) - if (foregroundColor != null) { - holder.text.setTextColor(foregroundColor!!) - holder.icon.setIcon(iicon, sizeDp = 18, color = foregroundColor!!) - } + holder.text.setTextColor(foregroundColor) + holder.icon.setIcon(iicon, sizeDp = 18, color = foregroundColor) + holder.container.background = SimpleRippleDrawable(foregroundColor, backgroundColor) holder.text.text = content } @@ -49,5 +51,6 @@ class SearchItem(val key: String, val content: String = key, val iicon: IIcon? = class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { val icon: ImageView by bindView(R.id.search_icon) val text: TextView by bindView(R.id.search_text) + val container: LinearLayout by bindView(R.id.search_item_frame) } }
\ No newline at end of file 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 6db75dc..2c3b0d3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/searchview/SearchView.kt @@ -5,10 +5,7 @@ import android.content.res.ColorStateList import android.support.annotation.ColorInt import android.support.annotation.IdRes import android.support.transition.AutoTransition -import android.support.v7.widget.AppCompatEditText -import android.support.v7.widget.CardView -import android.support.v7.widget.LinearLayoutManager -import android.support.v7.widget.RecyclerView +import android.support.v7.widget.* import android.util.AttributeSet import android.view.* import android.widget.FrameLayout @@ -37,15 +34,14 @@ class SearchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr) { - //configs inner class Configs { - var foregroundColor: Int = 0xdd000000.toInt() + var foregroundColor: Int = SearchItem.foregroundColor set(value) { if (field == value) return field = value tintForeground(value) } - var backgroundColor: Int = 0xfffafafa.toInt() + var backgroundColor: Int = SearchItem.backgroundColor set(value) { if (field == value) return field = value @@ -75,6 +71,15 @@ class SearchView @JvmOverloads constructor( var openListener: ((searchView: SearchView) -> Unit)? = null var closeListener: ((searchView: SearchView) -> Unit)? = null /** + * Draw a divider between the search bar and the suggestion items + * The divider is colored based on the foreground color + */ + var withDivider: Boolean = true + set(value) { + field = value + if (value) divider.visible() else divider.invisible() + } + /** * StringRes for a "no results found" item * If [results] is ever set to an empty list, it will default to * a list with one item with this string @@ -89,6 +94,18 @@ class SearchView @JvmOverloads constructor( * This builder acts on an observable, so you may switch threads, debounce, and do anything else that you require */ var textObserver: (observable: Observable<String>, searchView: SearchView) -> Unit = { _, _ -> } + /** + * Click event for suggestion items + * This event is only triggered when [key] is not blank (like in [noResultsFound] + */ + var onItemClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit = { _, _, _, _ -> } + /** + * Long click event for suggestion items + * This event is only triggered when [key] is not blank (like in [noResultsFound] + */ + var onItemLongClick: (position: Int, key: String, content: String, searchView: SearchView) -> Unit = { _, _, _, _ -> } + + } /** @@ -105,8 +122,12 @@ class SearchView @JvmOverloads constructor( else value) } - - fun clearResults() = context.runOnUiThread { adapter.clear() } + /** + * Empties the list on the UI thread + * Note that this does not include any animations + * Use results = listOf() for the animated version + */ + internal fun clearResults() = context.runOnUiThread { adapter.clear() } val configs = Configs() //views @@ -118,6 +139,7 @@ class SearchView @JvmOverloads constructor( private val progress: ProgressBar by bindView(R.id.search_progress) private val iconMic: ImageView by bindView(R.id.search_mic) private val iconClear: ImageView by bindView(R.id.search_clear) + 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 @@ -151,6 +173,16 @@ class SearchView @JvmOverloads constructor( } }) adapter = this@SearchView.adapter + (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //clear the fade between item changes + } + with(adapter) { + withSelectable(true) + withOnClickListener { _, _, item, position -> + if (item.key.isNotBlank()) configs.onItemClick(position, item.key, item.content, this@SearchView); true + } + withOnLongClickListener { _, _, item, position -> + if (item.key.isNotBlank()) configs.onItemLongClick(position, item.key, item.content, this@SearchView); true + } } textEvents = RxTextView.textChangeEvents(editText) .skipInitialValue() @@ -210,6 +242,7 @@ class SearchView @JvmOverloads constructor( iconMic.drawable.setTint(color) iconClear.drawable.setTint(color) SearchItem.foregroundColor = color + divider.setBackgroundColor(color.adjustAlpha(0.1f)) editText.tint(color) editText.setTextColor(ColorStateList.valueOf(color)) } @@ -249,12 +282,6 @@ class SearchView @JvmOverloads constructor( } } recycler.gone() -// card.circularHide(menuX, menuHalfHeight, offset = 100, duration = configs.revealDuration, -// onFinish = { -// configs.closeListener?.invoke(this) -// if (configs.shouldClearOnClose) editText.text.clear() -// recycler.gone() -// }) } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt index 5fa9150..9f74c86 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/TransitionUtils.kt @@ -15,5 +15,5 @@ class TransitionEndListener(val onEnd: (transition: Transition) -> Unit) : Trans } fun TransitionSet.addEndListener(onEnd: (transition: Transition) -> Unit) { - addListener(TransitionEndListener { onEnd }) + addListener(TransitionEndListener(onEnd)) }
\ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/SimpleRippleDrawable.kt b/library/src/main/kotlin/ca/allanwang/kau/views/SimpleRippleDrawable.kt new file mode 100644 index 0000000..fbaad1a --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/views/SimpleRippleDrawable.kt @@ -0,0 +1,17 @@ +package ca.allanwang.kau.views + +import android.content.res.ColorStateList +import android.graphics.drawable.ColorDrawable +import android.graphics.drawable.RippleDrawable +import android.support.annotation.ColorInt +import ca.allanwang.kau.searchview.SearchItem +import ca.allanwang.kau.utils.adjustAlpha + +/** + * Created by Allan Wang on 2017-06-24. + * + * Tries to mimic a standard ripple, given the foreground and background colors + */ +class SimpleRippleDrawable(@ColorInt foregroundColor: Int, @ColorInt backgroundColor: Int +) : RippleDrawable(ColorStateList(arrayOf(intArrayOf()), intArrayOf(foregroundColor)), + ColorDrawable(backgroundColor), ColorDrawable(foregroundColor.adjustAlpha(0.16f)))
\ No newline at end of file diff --git a/library/src/main/res/layout/kau_search_item.xml b/library/src/main/res/layout/kau_search_item.xml index fbdc826..20f3ef2 100644 --- a/library/src/main/res/layout/kau_search_item.xml +++ b/library/src/main/res/layout/kau_search_item.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/linearLayout" + android:id="@+id/search_item_frame" android:layout_width="match_parent" android:layout_height="@dimen/kau_search_item_height" android:background="?android:attr/selectableItemBackground" diff --git a/library/src/main/res/layout/kau_search_view.xml b/library/src/main/res/layout/kau_search_view.xml index 5626c4b..5c2ad27 100644 --- a/library/src/main/res/layout/kau_search_view.xml +++ b/library/src/main/res/layout/kau_search_view.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> @@ -14,8 +15,9 @@ android:id="@+id/search_cardview" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingEnd="2dp" - android:paddingStart="2dp"> + android:layout_marginEnd="@dimen/kau_search_base_margin" + android:layout_marginStart="@dimen/kau_search_base_margin" + app:cardCornerRadius="@dimen/kau_search_base_corners"> <LinearLayout android:layout_width="match_parent" @@ -23,7 +25,6 @@ android:orientation="vertical"> <LinearLayout - android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="@dimen/kau_search_height" android:focusable="true" diff --git a/library/src/main/res/transition/kau_auto.xml b/library/src/main/res/transition/kau_auto.xml deleted file mode 100644 index 8cda329..0000000 --- a/library/src/main/res/transition/kau_auto.xml +++ /dev/null @@ -1,4 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<autoTransition xmlns:android="http://schemas.android.com/apk/res/android" - android:duration="300" - android:interpolator="@android:interpolator/fast_out_slow_in" />
\ No newline at end of file diff --git a/library/src/main/res/values/dimens.xml b/library/src/main/res/values/dimens.xml index 0c0e749..cbb378a 100644 --- a/library/src/main/res/values/dimens.xml +++ b/library/src/main/res/values/dimens.xml @@ -14,6 +14,4 @@ <dimen name="kau_status_bar_height">24dp</dimen> - <dimen name="kau_search_dropdownitem_icon_width">32dip</dimen> - </resources> diff --git a/library/src/main/res/values/dimens_search.xml b/library/src/main/res/values/dimens_search.xml index 3664403..9d4ea5e 100644 --- a/library/src/main/res/values/dimens_search.xml +++ b/library/src/main/res/values/dimens_search.xml @@ -3,6 +3,8 @@ <dimen name="kau_search_height">46dp</dimen> + <dimen name="kau_search_base_margin">4dp</dimen> + <dimen name="kau_search_base_corners">2dp</dimen> <dimen name="kau_search_key_line_8">8dp</dimen> <dimen name="kau_search_key_line_16">16dp</dimen> <dimen name="kau_search_divider">1dp</dimen> 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 7680588..c836227 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt @@ -1,5 +1,6 @@ package ca.allanwang.kau.sample +import android.graphics.Color import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -136,6 +137,11 @@ class MainActivity : KPrefActivity() { } noResultsFound = R.string.kau_no_results_found shouldClearOnClose = false + onItemClick = { + position, key, content, searchView -> + toast(content) + searchView.revealClose() + } } return true } |