Skip to content

Commit

Permalink
Fix layout not refreshing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinGeorgeJoseph committed Mar 20, 2022
1 parent 022e3e5 commit 7712a32
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 31 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
findViewById<MaterialChipSetsContainer>(R.id.materialChipSetsContainer).apply {

chipSetSolidColorUnselected = ContextCompat.getColor(context, android.R.color.white)
chipSetSolidColorSelected = ContextCompat.getColor(context, R.color.selected_blue)
chipSetStrokeWidth = Utils.convertDpToPx(this@MainActivity, 2)
chipSetSolidColorUnselected = ContextCompat.getColor(context, android.R.color.black)
chipSetSolidColorSelected = ContextCompat.getColor(context, R.color.green)
chipSetStrokeWidth = Utils.convertDpToPx(this@MainActivity, 3)
chipSetStrokeColorUnselected =
ColorStateList.valueOf(ContextCompat.getColor(context, R.color.light_gray))
ColorStateList.valueOf(ContextCompat.getColor(context, R.color.green))
chipSetStrokeColorSelected =
ColorStateList.valueOf(ContextCompat.getColor(context, R.color.selected_blue))
ColorStateList.valueOf(ContextCompat.getColor(context, R.color.green))

chipTextColorUnselected = ContextCompat.getColor(context, android.R.color.black)
chipTextColorUnselected = ContextCompat.getColor(context, android.R.color.white)
chipTextColorSelected = ContextCompat.getColor(context, android.R.color.white)
chipTextDividerColorUnselected =
ContextCompat.getColor(context, android.R.color.darker_gray)
ContextCompat.getColor(context, R.color.green)

dataSet = createDataSet()

listener = object: MaterialChipsetWidgetListener {
override fun onChipSelectionChanged(name: String?, checked: Boolean) {

override fun onChipSelectionChanged(
name: String?,
chipSelectionGroup: String,
checked: Boolean,
) {
Toast.makeText(this@MainActivity,"onChipSelectionChanged --> $name || $checked",Toast.LENGTH_SHORT).show()

}

override fun onChipsetsResetClicked() {
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@color/black_04"
android:paddingStart="20dp"
android:paddingTop="50dp"
tools:context=".MainActivity">

<androidx.appcompat.widget.AppCompatTextView
android:textSize="22sp"
android:textColor="@color/white"
android:text="MaterialChipSetWidget"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<com.justin.materialchipsetwidget.MaterialChipSetsContainer
android:id="@+id/materialChipSetsContainer"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:scrollbars="none"
android:padding="10dp"
android:layout_height="wrap_content"/>
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<resources xmlns:tools="http://schemas.android.com/tools">
<color name="purple_200">@color/black</color>
<color name="purple_500">@color/black</color>
<color name="purple_700">@color/black</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>

<color name="green">#E98FE792</color>
<color name="green">#54A442</color>
<color name="darkGreen">#44EA2A</color>

<color name="selected_blue">#E9165FD3</color>
<color name="light_gray">@android:color/darker_gray</color>
<color name="black_04">#FF040404</color>

</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.CustomMaterialChipWidget" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<style name="Theme.CustomMaterialChipWidget" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
Expand Down
Binary file removed demo/1.png
Binary file not shown.
Binary file removed demo/2.png
Binary file not shown.
Binary file removed demo/customChipsetWidget.gif
Binary file not shown.
Binary file added demo/header_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.justin.materialchipsetwidget

data class ChipSetData(val position : Int, val chipList : List<String>, var isSelected : Boolean = false, var selectedPosition :Int =-1)
data class ChipSetData(val position : Int, val chipList : List<String>,var chipSelectionGroup:String="", var isSelected : Boolean = false, var selectedPosition :Int =-1)

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.justin.materialchipsetwidget

interface ChipSetWidgetListener {
fun onChipClicked(position : Int, title : String?, isSelected :Boolean)
}
fun onChipClicked(position : Int,chipSelectionGroup:String, title : String?, isSelected :Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MaterialChipSetWidget @JvmOverloads constructor(
) : LinearLayout(context, attrs) {

var chipSetWidgetListener : ChipSetWidgetListener? = null
var chipSelectionGroup:String=""

var chipDataSet : ChipSetData? = null
set(value) {
Expand Down Expand Up @@ -119,7 +120,7 @@ class MaterialChipSetWidget @JvmOverloads constructor(
if (this.title == value ) {
this.isSelected = !this.isSelected
chipDataSet?.let {
chipSetWidgetListener?.onChipClicked(it.position,this.title,this.isSelected)
chipSetWidgetListener?.onChipClicked(it.position,chipSelectionGroup,this.title,this.isSelected)
}
} else {
this.isSelected = false
Expand Down Expand Up @@ -175,4 +176,4 @@ class MaterialChipSetWidget @JvmOverloads constructor(
updateVisibilityOfChildren(chipDataList)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MaterialChipSetsContainer @JvmOverloads constructor(
chipSetDataList = chipSetList
}

private var chipSetDataList: List<ChipSetData>? = null
private var chipSetDataList: List<ChipSetData>? = null
set(value) {
field = value
refreshWidgetViews(field)
Expand All @@ -53,6 +53,7 @@ class MaterialChipSetsContainer @JvmOverloads constructor(
var chipTextColorUnselected: Int = ContextCompat.getColor(context, R.color.light_gray)
var chipTextColorSelected: Int = ContextCompat.getColor(context, R.color.light_gray)
var chipTextDividerColorUnselected = ContextCompat.getColor(context, R.color.light_gray)
val chipRadius:Float by lazy { Utils.convertDpToPx(context,48)}

@DrawableRes
var closeButtonIcon: Int = R.drawable.ic_baseline_close_24
Expand Down Expand Up @@ -82,7 +83,7 @@ class MaterialChipSetsContainer @JvmOverloads constructor(
}

containerLayout.addView(getCloseButton())

chipSetWidgetList.clear()
chipSetWidgetList.addAll(getMaterialChipSetWidgets(context, dataSet))
chipSetWidgetList.forEach { chipSetWidget ->
containerLayout.addView(chipSetWidget)
Expand Down Expand Up @@ -128,21 +129,24 @@ class MaterialChipSetsContainer @JvmOverloads constructor(
}
}
layoutParams = params
this.chipSelectionGroup=dataSet[index].chipSelectionGroup
this.chipSetWidgetListener = object : ChipSetWidgetListener {
override fun onChipClicked(position: Int, title: String?, isSelected: Boolean) {
updateChipsetWidgetStatus(position, isSelected)
sendChipStatusReturn(title,isSelected)
override fun onChipClicked(position: Int,chipSelectionGroup:String, title: String?, isSelected: Boolean) {
updateChipsetWidgetStatus(position, isSelected)
sendChipStatusReturn(title, chipSelectionGroup, isSelected)
}
}
this.chipDataSet = dataSet[index]
}
materialChipSetWidget.measure(0,0)
materialChipSetWidget.background = getBackGroundDrawable(dataSet[index].isSelected)

widgetList.add(materialChipSetWidget)
}
return widgetList
}

private fun sendChipStatusReturn(title : String?, selected: Boolean) = listener?.onChipSelectionChanged(title,selected)
private fun sendChipStatusReturn(title : String?,chipSelectionGroup:String, selected: Boolean) = listener?.onChipSelectionChanged(title,chipSelectionGroup,selected)

private fun updateChipsetWidgetStatus(position: Int, selected: Boolean) {
chipSetWidgetList[position].background = getBackGroundDrawable(selected)
Expand Down Expand Up @@ -220,12 +224,19 @@ class MaterialChipSetsContainer @JvmOverloads constructor(
solidColor: Int, strokeColor: ColorStateList,
strokeWidth: Float,
): Drawable {
val rectangleShape = RectangleShape(radius = resources.getDimension(R.dimen.radius)).apply {
val rectangleShape = RectangleShape(radius = chipRadius).apply {
this.backGroundTintColor = solidColor
this.strokeWidth = strokeWidth
this.strokeColor = strokeColor
}
return ShapeRender(rectangleShape).getDrawable()
}

}
public fun updateViews(chipSetDataList:List<ChipSetData>){
this.chipSetDataList=chipSetDataList
}
public fun getChipSetDataList(): List<ChipSetData>? {
return this.chipSetDataList
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.justin.materialchipsetwidget

interface MaterialChipsetWidgetListener {
fun onChipSelectionChanged(name : String?, checked : Boolean)

fun onChipSelectionChanged(name: String?, chipSelectionGroup: String, checked: Boolean)

fun onChipsetsResetClicked()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.graphics.drawable.Drawable
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel

class ShapeRender(val shape: Shape) {
class ShapeRender(private val shape: Shape) {

fun getDrawable(): Drawable {

Expand Down

0 comments on commit 7712a32

Please sign in to comment.