Skip to content

Commit

Permalink
- Added new switches
Browse files Browse the repository at this point in the history
  • Loading branch information
Stjin committed Feb 2, 2020
1 parent 762815d commit 03bc836
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Before you try to use this library

This library is forked and modified for the app [Cometin](https://cometin.stjin.host)
I tried making the library as functional for others but there might be some elements you don't like.

# ExpandableCardView

[ ![Download](https://api.bintray.com/packages/alespero/ExpandableCardView/ExpandableCardView/images/download.svg?version=0.8) ](https://jitpack.io/#Stjinchan/ExpandableCardView/)
Expand Down Expand Up @@ -69,6 +74,7 @@ app:expandableCardStrokeColor="@color/colorAccent"
app:expandableCardArrowColor="@color/colorAccent"
app:animationDuration="900"
app:startExpanded="true"
app:showSwitch="true"
```

### Java
Expand Down Expand Up @@ -98,13 +104,24 @@ card.setOnExpandedListener(new OnExpandedListener() {
Toast.makeText(applicationContext, isExpanded ? "Expanded!" : "Collapsed!", Toast.LENGTH_SHORT).show();
}
});

card.setOnSwitchChangeListener(new OnSwitchChangeListene() {
@Override
public void OnSwitchChangeListener(View v, boolean isChecked) {
Toast.makeText(applicationContext, isChecked ? "Checked!" : "Not checked!", Toast.LENGTH_SHORT).show();
}
});
```
### Kotlin

```kotlin
card.setOnExpandedListener { view, isExpanded ->
Toast.makeText(applicationContext, if(isExpanded) "Expanded!" else "Collapsed!", Toast.LENGTH_SHORT).show()
}

card.setOnSwitchChangeListener { view, isChecked ->
Toast.makeText(applicationContext, if(isChecked) "Checked!" else "Not checked!", Toast.LENGTH_SHORT).show()
}
```
## Contribute

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class MainActivity : AppCompatActivity() {
if(isExpanded) Toast.makeText(applicationContext, "Expanded!", Toast.LENGTH_SHORT).show()
else Toast.makeText(applicationContext, "Collapsed!", Toast.LENGTH_SHORT).show()
}

main_location.setOnSwitchChangeListener { _, isChecked ->
if(isChecked) Toast.makeText(applicationContext, "Checked!", Toast.LENGTH_SHORT).show()
else Toast.makeText(applicationContext, "not checked!", Toast.LENGTH_SHORT).show()
}
}


Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
app:expandOnClick="true"
app:showSwitch="true"
app:expandableCardElevation="200"
app:icon="@drawable/ic_location"
app:inner_view="@layout/location"
Expand Down
4 changes: 2 additions & 2 deletions expandablecardview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'kotlin-android-extensions'
ext {
PUBLISH_GROUP_ID = 'host.stjin'
PUBLISH_ARTIFACT_ID = 'expandable-cardview'
PUBLISH_VERSION = '1.0'
PUBLISH_VERSION = '1.1'
}

android {
Expand All @@ -15,7 +15,7 @@ android {
minSdkVersion 18
targetSdkVersion 29
versionCode 3
versionName "1.0"
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host.stjin.expandablecardview


import android.R.attr.button
import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
Expand All @@ -10,15 +11,18 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.view.animation.Transformation
import android.widget.CompoundButton
import android.widget.LinearLayout
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.expandable_cardview.view.*


/**
*
* Copyright (c) 2018 Alessandro Sperotti
Expand Down Expand Up @@ -52,6 +56,7 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
private var innerViewRes: Int = 0
private var cardStrokeColor: Int = android.R.color.transparent
private var cardArrowColor: Int = android.R.color.darker_gray
private var cardTextColor: Int = android.R.color.darker_gray
private var cardStrokeWidth: Int = 0
private var cardRadius: Float = 4f
private var cardElevation: Float = 4f
Expand All @@ -65,11 +70,13 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
private var isExpanding = false
private var isCollapsing = false
private var expandOnClick = false
private var showSwitch = false
private var startExpanded = false

private var previousHeight = 0

private var listener: OnExpandedListener? = null
private var switchListener: OnSwitchChangeListener? = null

private val defaultClickListener = OnClickListener {
if (isExpanded)
Expand All @@ -78,6 +85,8 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
expand()
}

private val switchOnCheckedChangeListener = CompoundButton.OnCheckedChangeListener { p0, p1 -> switchListener?.onSwitchChanged(p0, p1) }

private val isMoving: Boolean
get() = isExpanding || isCollapsing

Expand All @@ -102,6 +111,7 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
iconDrawable = typedArray.getDrawable(R.styleable.ExpandableCardView_icon)
innerViewRes = typedArray.getResourceId(R.styleable.ExpandableCardView_inner_view, View.NO_ID)
expandOnClick = typedArray.getBoolean(R.styleable.ExpandableCardView_expandOnClick, false)
showSwitch = typedArray.getBoolean(R.styleable.ExpandableCardView_showSwitch, false)
animDuration = typedArray.getInteger(R.styleable.ExpandableCardView_animationDuration, DEFAULT_ANIM_DURATION).toLong()
startExpanded = typedArray.getBoolean(R.styleable.ExpandableCardView_startExpanded, false)
cardRipple = typedArray.getBoolean(R.styleable.ExpandableCardView_expandableCardRipple, false)
Expand All @@ -110,6 +120,7 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
cardStrokeWidth = typedArray.getInteger(R.styleable.ExpandableCardView_expandableCardStrokeWidth, 0)
cardElevation = typedArray.getFloat(R.styleable.ExpandableCardView_expandableCardElevation, 4f)
cardRadius = typedArray.getFloat(R.styleable.ExpandableCardView_expandableCardRadius, 4f)
cardTextColor = typedArray.getInteger(R.styleable.ExpandableCardView_expandableCardTitleColor, android.R.color.darker_gray)

typedArray.recycle()
}
Expand All @@ -124,6 +135,8 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
if (!cardRipple) card_layout.rippleColor = ContextCompat.getColorStateList(context, android.R.color.transparent)


card_switch.visibility = if (showSwitch) View.VISIBLE else View.GONE
card_title.setTextColor(cardArrowColor)
card_layout.radius = Utils.convertPixelsToDp(cardRadius, context)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -150,6 +163,10 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
card_arrow.setOnClickListener(defaultClickListener)
}

if (showSwitch) {
card_switch.setOnCheckedChangeListener(switchOnCheckedChangeListener)
}

}

private fun expand() {
Expand Down Expand Up @@ -234,8 +251,39 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
card_arrow.startAnimation(arrowAnimation)
isExpanded = animationType == EXPANDING


if (showSwitch) {
val switchAnimation = if (animationType == EXPANDING)
AlphaAnimation(1f, 0f)
else
AlphaAnimation(0f, 1f)
switchAnimation.duration = animDuration
switchAnimation.fillAfter = true
card_switch.startAnimation(switchAnimation)

// Disable/Set clickable to prevent the toggle from working when the card is expanded
switchAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {
//
}

override fun onAnimationEnd(p0: Animation?) {
card_switch.isClickable = animationType != EXPANDING
card_switch.isEnabled = animationType != EXPANDING
}

override fun onAnimationStart(p0: Animation?) {
if (animationType != EXPANDING) {
card_switch.isEnabled = animationType != EXPANDING
}
card_switch.isClickable = animationType != EXPANDING

}
})
}
}


fun setOnExpandedListener(listener: OnExpandedListener) {
this.listener = listener
}
Expand All @@ -252,6 +300,24 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
this.listener = null
}


fun setOnSwitchChangeListener(listener: OnSwitchChangeListener) {
this.switchListener = listener
}

fun setOnSwitchChangeListener(method: (v: View?, isChecked: Boolean) -> Unit) {
this.switchListener = object : OnSwitchChangeListener {
override fun onSwitchChanged(v: View?, isChecked: Boolean) {
method(v, isChecked)
}

}
}

fun removeOnSwitchChangeListener() {
this.switchListener = null
}

fun setTitle(@StringRes titleRes: Int = -1, titleText: String = "") {
if (titleRes != -1)
card_title.setText(titleRes)
Expand All @@ -276,7 +342,7 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
}


override fun setOnClickListener(l: View.OnClickListener?) {
override fun setOnClickListener(l: OnClickListener?) {
card_arrow.setOnClickListener(l)
super.setOnClickListener(l)
}
Expand All @@ -287,9 +353,11 @@ class ExpandableCardView @JvmOverloads constructor(context: Context, attrs: Attr
*/

interface OnExpandedListener {

fun onExpandChanged(v: View?, isExpanded: Boolean)
}

interface OnSwitchChangeListener {
fun onSwitchChanged(v: View?, isChecked: Boolean)
}

companion object {
Expand Down
14 changes: 12 additions & 2 deletions expandablecardview/src/main/res/layout/expandable_cardview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,33 @@
android:id="@+id/card_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_toEndOf="@+id/card_icon"
android:maxLength="39"
android:maxLines="1"
android:textSize="16sp"
tools:text="Prova" />

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/card_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="8dp"
android:layout_toStartOf="@id/card_arrow" />

<ImageButton
android:id="@+id/card_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="20dp"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/expandable_card_view_image_content_description"
android:tint="@android:color/black"
android:src="@drawable/arrow_down" />
android:src="@drawable/arrow_down"
android:tint="@android:color/black" />

</RelativeLayout>

Expand Down
2 changes: 2 additions & 0 deletions expandablecardview/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<attr name="icon" format="reference"/>
<attr name="inner_view" format="reference"/>
<attr name="expandOnClick" format="boolean"/>
<attr name="showSwitch" format="boolean"/>
<attr name="animationDuration" format="integer"/>
<attr name="startExpanded" format="boolean"/>
<attr name="expandableCardRipple" format="boolean"/>
<attr name="expandableCardStrokeWidth" format="integer"/>
<attr name="expandableCardStrokeColor" format="reference"/>
<attr name="expandableCardArrowColor" format="reference"/>
<attr name="expandableCardTitleColor" format="reference"/>
<attr name="expandableCardElevation" format="float"/>
<attr name="expandableCardRadius" format="integer"/>
</declare-styleable>
Expand Down

0 comments on commit 03bc836

Please sign in to comment.