CheckableChipView

综合技术 2018-08-30 阅读原文

CheckableChipView

A checkable widget for Android. Based on the EventFilterView from the Google I/O 2018 app .

Requires Android minSdkVersion 21.

Setup

Make sure you have the jcenter repo in your project level build.gradle

allprojects {
    repositories {
        jcenter()
    }
}

Add the dependency to your lib/app build.gradle

dependencies {
    implementation 'com.github.okdroid:checkablechipview:1.0.2'
}

Usage

XML

Include the widget into your xml layout like this

The following xml attributes are supported

AttributeDescription
android:textThe text to display
android:textColorThe default text color
android:colorThe color of the indicator dot as well as the background color when the widget is checked
android:backgroundThe default background color
android:checked The checked state of the widget, either true or false
app:ccv_outlineColorThe color of the outline
app:ccv_outlineCornerRadiusThe corner radius of the outline, in dp. Defaults to a pill shape if not set
app:ccv_outlineWidthThe stroke width of the outline, in dp
app:ccv_checkedTextColorThe text color when the widget is checked
app:ccv_foregroundThe foreground drawable to display

In code

The state of the widget can be observed like this

chip.onCheckedChangeListener = { view: CheckableChipView, isChecked: Boolean ->
    // do your logic here
}

To switch between checked/unchecked state programatically with animation, use the following method:

chip.setCheckedAnimated(checked = true) {
    // onAnimationEnd callback
}

Plus, there are following methods at your service for changing the state without animation:

if (!chip.isChecked) {
    chip.isChecked = true
}

chip.toggle() // toggles between states

Acknowledgements

Thanks to the team behind the Google I/O app . Thank you for open sourcing the code and letting others reuse and learn from it.

Thanks to Nick Butcher for the initial implementation in the I/O app.

License

Copyright 2018 Google, Inc.
Copyright 2018 okdroid

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
The Android Arsenal

责编内容by:The Android Arsenal阅读原文】。感谢您的支持!

您可能感兴趣的

Android 动画使用详解(二) 补间动画 动画在Android 开发中经常会被用到,好的动画效果可以达到事半功倍的效果。补间动画也是Android中常用的动画之一,相对属性动画来说,补间动画的点击事件不会跟着动画的位置变化而变化。后续将逐渐被属性动画替代。 透明动画 A...
Convert text to image file on Android I have a text document (.txt). I want to convert it to an image (.png or .jpg). For example, black text on white back...
命令行程序解密 Android 微信数据库 命令行程序解密 Android 微信数据库 Android 微信使用了 SQLCipher 对消息数据库进行加密,最近还把数据库作为一个...
android 多线程编程 android 中的线程基于 linux 的线程,当我们启动一个 App 的时候,Android系统会启动一个Linux Process,该 Process 包含一个 Thread,称为UI Thread或Main Thread。 ...
美团App 插件化实践 背景 在Android开发行业里,插件化已经不是一门新鲜的技术了,在稍大的平台型App上早已是标配。进入2017年,Atlas、Replugin、VirtualAPK相继开源,标志着插件化技术进入了成熟阶段。但纵观各大插件框架,都是基于...