Skip to content

Commit

Permalink
Make ExpandableText respect enabled state, use a CheckBox as widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 committed Jan 5, 2019
1 parent 87f26b8 commit 1e7a170
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.transition.ChangeBounds
import android.transition.TransitionManager
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.CheckBox
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
Expand All @@ -42,7 +42,7 @@ class ExpandableTextPreference(key: String) : Preference(key) {

override fun bindViews(holder: PreferencesAdapter.ViewHolder) {
super.bindViews(holder)
val widget = holder.widget as ImageView
val widget = holder.widget as CheckBox
val inflater = LayoutInflater.from(widget.context)
val tv = (widget.tag ?: inflater.inflate(R.layout.map_preference_expand_text, holder.root)
.findViewById(android.R.id.message)) as TextView
Expand All @@ -54,25 +54,20 @@ class ExpandableTextPreference(key: String) : Preference(key) {
setBackgroundColor(a.getColor(0, ContextCompat.getColor(context,
R.color.expandableTextBackgroundColorDefault)))
a.recycle()
isEnabled = enabled
}

refreshArrowState(widget, false)
widget.post { refreshArrowState(widget) }
refreshArrowState(widget)
refreshTextExpandState(tv)
}

override fun onClick(holder: PreferencesAdapter.ViewHolder) {
expanded = !expanded
refreshArrowState(holder.widget as ImageView)
refreshArrowState(holder.widget as CheckBox)
refreshTextExpandState(holder.widget.tag as TextView)
}

/**
* Update expand/collapse arrow
*/
private fun refreshArrowState(widget: ImageView, exp: Boolean = expanded) {
val drawableState = if (exp) intArrayOf(android.R.attr.state_checked) else null
widget.setImageState(drawableState, exp)
private fun refreshArrowState(widget: CheckBox) {
widget.isChecked = expanded
}

private fun refreshTextExpandState(text: TextView) {
Expand Down

This file was deleted.

17 changes: 17 additions & 0 deletions library/src/main/res/drawable/map_collapse_to_expand_animation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:drawable="@drawable/map_ic_collapse_24dp">
<target android:name="path">
<aapt:attr name="android:animation">
<set>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/path_ic_collapse"
android:valueTo="@string/path_ic_expand"
android:valueType="pathType" />
</set>
</aapt:attr>
</target>
</animated-vector>
18 changes: 18 additions & 0 deletions library/src/main/res/drawable/map_expand_animated_selector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/map_btn_checked"
android:drawable="@drawable/map_ic_collapse_24dp"
android:state_checked="true" />
<item
android:id="@+id/map_btn_unchecked"
android:drawable="@drawable/map_ic_expand_24dp" />
<transition
android:drawable="@drawable/map_expand_to_collapse_animation"
android:fromId="@id/map_btn_unchecked"
android:toId="@id/map_btn_checked" />
<transition
android:drawable="@drawable/map_collapse_to_expand_animation"
android:fromId="@id/map_btn_checked"
android:toId="@id/map_btn_unchecked" />
</animated-selector>
17 changes: 17 additions & 0 deletions library/src/main/res/drawable/map_expand_to_collapse_animation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:drawable="@drawable/map_ic_expand_24dp">
<target android:name="path">
<aapt:attr name="android:animation">
<set>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="pathData"
android:valueFrom="@string/path_ic_expand"
android:valueTo="@string/path_ic_collapse"
android:valueType="pathType" />
</set>
</aapt:attr>
</target>
</animated-vector>
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.AppCompatImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="32dp"
android:scaleType="center"
android:src="@drawable/map_animated_expand_vector_selector"
android:tint="?android:textColorPrimary"
tools:ignore="ContentDescription" />
android:background="@null"
android:button="@drawable/map_expand_animated_selector"
android:buttonTint="?android:textColorPrimary"
android:clickable="false" />

0 comments on commit 1e7a170

Please sign in to comment.