Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multiple selection function #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions .idea/codeStyles/Project.xml

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

1 change: 1 addition & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions constraint-radiogroup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -31,7 +35,9 @@ class ConstraintRadioGroup @JvmOverloads constructor(
var unSelectedTextTypeface: Typeface? = null
private set

private var checkedButton: CompoundButton? = null
private val checkedButtons: ArrayList<CompoundButton> = arrayListOf()

private var isMultiple: Boolean = false

init {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ConstraintRadioGroup)
Expand All @@ -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)
Expand Down Expand Up @@ -74,22 +82,38 @@ 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)
}

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)
}
}
}

Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import androidx.annotation.IdRes


interface OnCheckedChangeListener {
fun onCheckedChanged(group: ConstraintRadioGroup, checkedButton: CompoundButton)
fun onCheckedChanged(group: ConstraintRadioGroup, checkedButton: CompoundButton, isChecked: Boolean)
}
1 change: 1 addition & 0 deletions constraint-radiogroup/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<attr name="unSelected_font" format="reference"/>
<attr name="selected_text_color" format="color|reference" />
<attr name="unSelected_text_color" format="color|reference" />
<attr name="is_multiple_select" format="boolean" />
</declare-styleable>
</resources>
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion publish.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'com.github.panpf.bintray-publish'


publish {
Expand Down