+
\ No newline at end of file
diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
index 8f1a3b7..407ade2 100644
--- a/.idea/codeStyles/codeStyleConfig.xml
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -1,5 +1,6 @@
+
\ No newline at end of file
diff --git a/app/build.gradle b/app/build.gradle
index 7e73efb..4dc9ac9 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -3,13 +3,13 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
- compileSdkVersion 29
- buildToolsVersion "29.0.3"
+ compileSdkVersion 30
+ buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.wayne.constraintradiogroup.demo"
- minSdkVersion 23
- targetSdkVersion 29
+ minSdkVersion 14
+ targetSdkVersion 30
versionCode 1
versionName "1.0"
}
diff --git a/app/src/main/java/com/wayne/constraintradiogroup/demo/MainActivity.kt b/app/src/main/java/com/wayne/constraintradiogroup/demo/MainActivity.kt
index 4f7ea66..3823df0 100644
--- a/app/src/main/java/com/wayne/constraintradiogroup/demo/MainActivity.kt
+++ b/app/src/main/java/com/wayne/constraintradiogroup/demo/MainActivity.kt
@@ -15,7 +15,8 @@ class MainActivity : AppCompatActivity() {
crg_custom.checkedChangeListener = object : OnCheckedChangeListener {
override fun onCheckedChanged(
group: ConstraintRadioGroup,
- checkedButton: CompoundButton
+ checkedButton: CompoundButton,
+ isChecked: Boolean
) {
// TODO
// (checkedButton as RadioButton)
@@ -25,7 +26,8 @@ class MainActivity : AppCompatActivity() {
crg_check_box.checkedChangeListener = object : OnCheckedChangeListener {
override fun onCheckedChanged(
group: ConstraintRadioGroup,
- checkedButton: CompoundButton
+ checkedButton: CompoundButton,
+ isChecked: Boolean
) {
// TODO
// (checkedButton as CheckBox)
diff --git a/build.gradle b/build.gradle
index f12d1ae..f90da04 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,14 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
- ext.kotlin_version = "1.3.72"
+ ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
- classpath "com.android.tools.build:gradle:3.6.1"
+ classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- classpath 'com.novoda:bintray-release:0.9.2'
+ classpath 'com.github.panpf.bintray-publish:bintray-publish:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
diff --git a/constraint-radiogroup/build.gradle b/constraint-radiogroup/build.gradle
index 411a9f4..e04d49b 100644
--- a/constraint-radiogroup/build.gradle
+++ b/constraint-radiogroup/build.gradle
@@ -3,11 +3,11 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
- compileSdkVersion 29
+ compileSdkVersion 30
defaultConfig {
- minSdkVersion 23
- targetSdkVersion 29
+ minSdkVersion 14
+ targetSdkVersion 30
consumerProguardFiles "consumer-rules.pro"
}
@@ -24,7 +24,6 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
- implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
}
apply from: rootProject.file('publish.gradle')
\ No newline at end of file
diff --git a/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/ConstraintRadioGroup.kt b/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/ConstraintRadioGroup.kt
index 9c9d906..93e054e 100644
--- a/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/ConstraintRadioGroup.kt
+++ b/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/ConstraintRadioGroup.kt
@@ -9,15 +9,19 @@ import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
+import android.widget.CheckBox
import android.widget.CompoundButton
+import android.widget.RadioButton
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.res.ResourcesCompat
+import androidx.core.view.children
class ConstraintRadioGroup @JvmOverloads constructor(
context: Context,
- attrs: AttributeSet? = null
-) : ConstraintLayout(context, attrs) {
+ attrs: AttributeSet? = null,
+ defStyleAttr: Int = 0,
+) : ConstraintLayout(context, attrs, defStyleAttr) {
var checkedChangeListener: OnCheckedChangeListener? = null
@@ -31,7 +35,9 @@ class ConstraintRadioGroup @JvmOverloads constructor(
var unSelectedTextTypeface: Typeface? = null
private set
- private var checkedButton: CompoundButton? = null
+ private val checkedButtons: ArrayList = arrayListOf()
+
+ private var isMultiple: Boolean = false
init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ConstraintRadioGroup)
@@ -44,6 +50,8 @@ class ConstraintRadioGroup @JvmOverloads constructor(
unSelectedTextColor =
typedArray.getColor(R.styleable.ConstraintRadioGroup_unSelected_text_color, Color.BLACK)
+ isMultiple = typedArray.getBoolean(R.styleable.ConstraintRadioGroup_is_multiple_select, false)
+
try {
selectedTextTypeface = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
typedArray.getFont(R.styleable.ConstraintRadioGroup_selected_font)
@@ -74,13 +82,13 @@ class ConstraintRadioGroup @JvmOverloads constructor(
override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
if (child is CompoundButton) {
- if (child.isChecked) {
- checkedButton = child
+ if (child.isChecked && checkedButtons.count { it is RadioButton } == 1) {
+ checkedButtons.add(child)
}
setCheckedStateForView(child, false)
child.setOnCheckedChangeListener { buttonView, isChecked ->
- setCheckedButton(buttonView)
+ setCheckedButton(buttonView, isChecked)
}
}
super.addView(child, index, params)
@@ -88,8 +96,24 @@ class ConstraintRadioGroup @JvmOverloads constructor(
override fun onFinishInflate() {
super.onFinishInflate()
- checkedButton?.let {
- setCheckedStateForView(it, true)
+ if(children.all { it is RadioButton }) {
+ isMultiple = false
+ // Default choose first
+ checkedButtons.firstOrNull()?.let {
+ checkedButtons.clear()
+ checkedButtons.add(it)
+ }
+ checkedButtons.singleOrNull()?.let {
+ setCheckedStateForView(it, true)
+ }
+ } else if(!isMultiple) {
+ checkedButtons.singleOrNull()?.let {
+ setCheckedStateForView(it, true)
+ }
+ } else {
+ checkedButtons.forEach {
+ setCheckedStateForView(it, true)
+ }
}
}
@@ -104,18 +128,51 @@ class ConstraintRadioGroup @JvmOverloads constructor(
compoundButton.isChecked = checked
}
- private fun setCheckedButton(compoundButton: CompoundButton) {
- val changed = checkedButton != compoundButton
+ private fun setCheckedButton(compoundButton: CompoundButton, isChecked: Boolean) {
+ if(!isMultiple) {
+ val changed = checkedButtons.singleOrNull() != compoundButton
+
+ if (changed) {
+ checkedChangeListener?.onCheckedChanged(this, compoundButton, isChecked)
+
+ setCheckedStateForView(compoundButton, true)
+
+ checkedButtons.singleOrNull()?.let {
+ setCheckedStateForView(it, false)
+ }
+ checkedButtons.clear()
+ checkedButtons.add(compoundButton)
+ } else if (compoundButton is CheckBox) {
+ checkedChangeListener?.onCheckedChanged(this, compoundButton, isChecked)
+ setCheckedStateForView(compoundButton, isChecked)
+ if (isChecked) {
+ checkedButtons.clear()
+ checkedButtons.add(compoundButton)
+ } else checkedButtons.remove(compoundButton)
+ }
+ } else {
+ if (compoundButton is RadioButton && children.count { it is RadioButton } > 1) {
+ val changed = checkedButtons.firstOrNull { it is RadioButton } != compoundButton
+
+ if (changed) {
+ checkedChangeListener?.onCheckedChanged(this, compoundButton, isChecked)
+
+ setCheckedStateForView(compoundButton, true)
- if (changed) {
- checkedChangeListener?.onCheckedChanged(this, compoundButton)
+ checkedButtons.firstOrNull { it is RadioButton }?.let {
+ setCheckedStateForView(it, false)
+ }
+ checkedButtons.removeAll { it is RadioButton }
+ checkedButtons.add(compoundButton)
+ }
+ } else {
+ checkedChangeListener?.onCheckedChanged(this, compoundButton, isChecked)
- setCheckedStateForView(compoundButton, true)
+ setCheckedStateForView(compoundButton, isChecked)
- checkedButton?.let {
- setCheckedStateForView(it, false)
+ if (isChecked) checkedButtons.add(compoundButton)
+ else checkedButtons.remove(compoundButton)
}
- checkedButton = compoundButton
}
}
}
\ No newline at end of file
diff --git a/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/OnCheckedChangeListener.kt b/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/OnCheckedChangeListener.kt
index 6ea4f01..926fc38 100644
--- a/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/OnCheckedChangeListener.kt
+++ b/constraint-radiogroup/src/main/java/com/wayne/constraintradiogroup/OnCheckedChangeListener.kt
@@ -5,5 +5,5 @@ import androidx.annotation.IdRes
interface OnCheckedChangeListener {
- fun onCheckedChanged(group: ConstraintRadioGroup, checkedButton: CompoundButton)
+ fun onCheckedChanged(group: ConstraintRadioGroup, checkedButton: CompoundButton, isChecked: Boolean)
}
\ No newline at end of file
diff --git a/constraint-radiogroup/src/main/res/values/attrs.xml b/constraint-radiogroup/src/main/res/values/attrs.xml
index 6275049..1b4724f 100644
--- a/constraint-radiogroup/src/main/res/values/attrs.xml
+++ b/constraint-radiogroup/src/main/res/values/attrs.xml
@@ -5,5 +5,6 @@
+
\ No newline at end of file
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 3aa7815..ec2a601 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
diff --git a/publish.gradle b/publish.gradle
index 17e4db1..3af1381 100644
--- a/publish.gradle
+++ b/publish.gradle
@@ -1,4 +1,4 @@
-apply plugin: 'com.novoda.bintray-release'
+apply plugin: 'com.github.panpf.bintray-publish'
publish {