Depracated - use Anko by jetbrains
A collection of Kotlin extensions for Android, based on KotlinAndroidLib and Android Kotlin Extensions.
| apply plugin: 'android' | |
| android { | |
| compileSdkVersion 19 | |
| buildToolsVersion "19.0.3" | |
| defaultConfig { | |
| minSdkVersion 14 | |
| targetSdkVersion 19 | |
| versionCode 23 |
| /** | |
| * ANY | |
| */ | |
| fun Any.toJson(): String = GsonInitializer.toJson(this) | |
| /** | |
| * VIEW | |
| */ | |
| fun View.toggleVisibility(setAsVisible: Boolean) = if (setAsVisible) this.visible() else this.gone() |
Depracated - use Anko by jetbrains
A collection of Kotlin extensions for Android, based on KotlinAndroidLib and Android Kotlin Extensions.
| service: service-name | |
| provider: | |
| name: aws | |
| runtime: nodejs6.10 | |
| functions: | |
| myfunc: | |
| handler: handler.myfunc |
| class AnkoViewHolder<out V : AnkoComponent<ViewGroup>>(val parent: ViewGroup, val createUI: () -> V) { | |
| class ViewHolder<out V : AnkoComponent<ViewGroup>>(val view: View, val ui: V) : RecyclerView.ViewHolder(view) | |
| val ankoContext by lazy { AnkoContext.createReusable(parent.context, parent) } | |
| val viewHolder by lazy { | |
| val ui = createUI() | |
| val view = ui.createView(ankoContext) | |
| view.setupTapEffectForBG(true) // default for every item in application | |
| // TODO some items may be disabled... | |
| ViewHolder(view, ui) |
| class AnkoUIMaker(val context: Context) { | |
| private val constraintlayoutID = R.id.ankoConstraintLayout | |
| fun UiMake(): View = | |
| context.constraintLayout { | |
| id = constraintlayoutID | |
| // 추가 | |
| textView("Anko Hello").lparams(wrapContent, wrapContent) { | |
| startToStart = constraintlayoutID | |
| endToEnd = constraintlayoutID | |
| topToTop = constraintlayoutID |
| inline fun <V : View> V.alpha(alpha: Double): V { | |
| this.alpha = alpha.toFloat() | |
| return this | |
| } | |
| inline fun <V : View> V.backgroundColor(color: Int): V { | |
| backgroundColor = color | |
| return this | |
| } |
| abstract class BaseFragment<VB : ViewDataBinding, VM : BaseViewModel> : DaggerFragment() { | |
| abstract fun getViewModelClass(): Class<VM> | |
| abstract fun layoutId(): Int | |
| @Inject | |
| lateinit var viewModelFactory: ViewModelProvider.Factory | |
| protected lateinit var binding: VB | |
| protected lateinit var viewModel: VM |
| class MainViewModel { | |
| val visible = ObservableBoolean(false) | |
| } |
| /** | |
| * Extends LiveData allowing Kotlin DSL for onChanged callback usage | |
| */ | |
| fun <T> LiveData<T>.observeNonNull(lifecycleOwner: LifecycleOwner, onItem: (T) -> Unit) { | |
| this.observe(lifecycleOwner, object : NonNullObserver<T> { | |
| override fun onChangedNonNull(t: T) { | |
| onItem(t) | |
| } | |
| }) | |
| } |