kotlin

How to handle both OnTouchListener event and OnClickListener event

浪子不回头ぞ 提交于 2020-12-01 11:56:25
问题 I have RecyclerView and I set to each item OnTouchListener and OnClickListener OnTouchListener: override fun onBindViewHolder(holder: ItemListViewHolder, position: Int) { val item = getItem(position) holder.bind(item) holder.itemView.cardview.setOnTouchListener { v, event -> if (event.action == MotionEvent.ACTION_DOWN) { Log.d(TAG, "onBindViewHolder: $event.buttonState") this.startDragListener.onStartDrag(holder) } return@setOnTouchListener true } } OnClickListener: (inside the view holder)

Make asynchronous call synchronous in Kotlin

六眼飞鱼酱① 提交于 2020-12-01 11:03:33
问题 I have an API I have no control over.... which contains a method that does some work and returns the results asynchronously. I would like to call this method synchronously in parts of my application. I have done this by adding a class ResultHandler which captures and returns the result. Is there a better way of doing this than the way I've done it below? Perhaps using standard kotlin (or Java as last resort) library methods. My preference would be for awaitReply to return the result and also

How to correctly handle Byte values greater than 127 in Kotlin?

核能气质少年 提交于 2020-11-30 20:40:32
问题 Imagine I have a Kotlin program with a variable b of type Byte , into which an external system writes values greater than 127 . "External" means that I cannot change the type of the value it returns. val a:Int = 128 val b:Byte = a.toByte() Both a.toByte() and b.toInt() return -128 . Imagine I want to get the correct value ( 128 ) from the variable b . How can I do it? In other words: What implementation of magicallyExtractRightValue would make the following test run? @Test fun

How to correctly handle Byte values greater than 127 in Kotlin?

你。 提交于 2020-11-30 20:34:32
问题 Imagine I have a Kotlin program with a variable b of type Byte , into which an external system writes values greater than 127 . "External" means that I cannot change the type of the value it returns. val a:Int = 128 val b:Byte = a.toByte() Both a.toByte() and b.toInt() return -128 . Imagine I want to get the correct value ( 128 ) from the variable b . How can I do it? In other words: What implementation of magicallyExtractRightValue would make the following test run? @Test fun

知识点 | ViewModel 四种集成方式

余生颓废 提交于 2020-11-30 20:33:56
ViewModel 库一发布,便成为了 Jetpack 中的核心组件之一。我们在 2019 年做的一份开发者问卷显示,超过 40% 的 Android 开发者已经在自己的应用中使用了 ViewModel。ViewModel 可以将数据层与 UI 分离,而这种架构不仅可以简化 UI 的生命周期的控制,也能让代码获得更好的可测试性。如果想了解更多,可以参考 ViewModel: 简单介绍视频 和 官方文档 。 由于 ViewModel 是许多功能实现的基础,我们在过去的几年里做了许多工作来改进 ViewModel 的易用性,也让它能够更加简便地与其他组件库相结合。下面的文章中,我将介绍 ViewModel 的四种集成方式: ViewModel 中的 Saved State —— 后台进程重启时,ViewModel 的数据恢复; 在 NavGraph 中使用 ViewModel —— ViewModel 与导航 (Navigation) 组件库的集成; ViewModel 配合数据绑定 (data-binding) —— 通过使用 ViewModel 和 LiveData 简化数据绑定; viewModelScope —— Kotlin 协程与 ViewModel 的集成。 ViewModel 的 Saved State —— 后台进程重启时,ViewModel 的数据恢复 于

How to correctly handle Byte values greater than 127 in Kotlin?

与世无争的帅哥 提交于 2020-11-30 20:33:04
问题 Imagine I have a Kotlin program with a variable b of type Byte , into which an external system writes values greater than 127 . "External" means that I cannot change the type of the value it returns. val a:Int = 128 val b:Byte = a.toByte() Both a.toByte() and b.toInt() return -128 . Imagine I want to get the correct value ( 128 ) from the variable b . How can I do it? In other words: What implementation of magicallyExtractRightValue would make the following test run? @Test fun

Modifying PagedList in Android Paging Architecture library

一个人想着一个人 提交于 2020-11-30 04:46:26
问题 I'm currently looking into incorporating the Paging Architecture library (version 2.1.0-beta01 at the time of writing) into my app. One components is a list which allows the user to delete individual items from it. This list is network-only and caching localy with Room does not make sense. PagedList is immutable and does not support modification. I have read that having a copy of the list which is than modified and returned as the new one is the way to go. The documentation states the same:

Modifying PagedList in Android Paging Architecture library

天涯浪子 提交于 2020-11-30 04:45:57
问题 I'm currently looking into incorporating the Paging Architecture library (version 2.1.0-beta01 at the time of writing) into my app. One components is a list which allows the user to delete individual items from it. This list is network-only and caching localy with Room does not make sense. PagedList is immutable and does not support modification. I have read that having a copy of the list which is than modified and returned as the new one is the way to go. The documentation states the same:

Modifying PagedList in Android Paging Architecture library

不打扰是莪最后的温柔 提交于 2020-11-30 04:44:56
问题 I'm currently looking into incorporating the Paging Architecture library (version 2.1.0-beta01 at the time of writing) into my app. One components is a list which allows the user to delete individual items from it. This list is network-only and caching localy with Room does not make sense. PagedList is immutable and does not support modification. I have read that having a copy of the list which is than modified and returned as the new one is the way to go. The documentation states the same:

Modifying PagedList in Android Paging Architecture library

心不动则不痛 提交于 2020-11-30 04:43:33
问题 I'm currently looking into incorporating the Paging Architecture library (version 2.1.0-beta01 at the time of writing) into my app. One components is a list which allows the user to delete individual items from it. This list is network-only and caching localy with Room does not make sense. PagedList is immutable and does not support modification. I have read that having a copy of the list which is than modified and returned as the new one is the way to go. The documentation states the same: