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 /library/src/main/kotlin/ca/allanwang | |
parent | 6ad176dde0a84a0eb96dea2f9c7eb34394045526 (diff) | |
download | kau-35b26d771199c1dbf0d67377627c9d8b27da388c.tar.gz kau-35b26d771199c1dbf0d67377627c9d8b27da388c.tar.bz2 kau-35b26d771199c1dbf0d67377627c9d8b27da388c.zip |
Create tintable ripples
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang')
4 files changed, 68 insertions, 21 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 |