我正在尝试使用协程创建一个流程,但它没有给我预期的结果。我想要的是给出一个过期时间(无论是否以毫秒、秒等为单位都没关系),当时间到达 0 时,它会停止倒计时。我现在拥有的是:
private fun tickerFlow(start: Long, end: Long = 0L) = flow {
var count = start
while (count >= end) {
emit(Unit)
count--
delay(1_000L)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将此函数称为:
val expireDate = LocalDateTime.now().plusSeconds(10L).toEpochSecond(ZoneOffset.UTC)
tickerFlow(expireDate)
.map { LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - expireDate }
.distinctUntilChanged { old, new ->
old == new
}
.onEach {
//Here I should print the timer going down with this pattern
//00h: 00m: 00s I did it with String.format("%02dh: %02dm: %02ds") and it works though.
}
.onCompletion {
//Setting …Run Code Online (Sandbox Code Playgroud) 我从数据绑定开始,但是一旦创建函数,我就无法运行该项目onClick,这是我的layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="userViewModel"
type="com.myproject.example.UserViewModel" />
</data>
....
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@={userViewModel.saveOrUpdateButton}"
android:onClick="@={() -> userViewModel.saveOrUpdate()}"
/>
Run Code Online (Sandbox Code Playgroud)
仅当我运行它时才出现的问题./gradlew :app:kaptDebugKotlin --stacktrace指向 onClick() 方法
还有这个
引起原因:org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException:执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障
导致:org.jetbrains.kotlin.kapt3.base.util.KaptBaseError:注释处理时出现异常
我的build.gradle包含:
应用插件:'kotlin-kapt'
数据绑定 { 启用 = true }
以及依赖项:
dependencies {
def lifecycle_version = "2.2.0"
def room_version = "2.2.3"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation …Run Code Online (Sandbox Code Playgroud) 例如,我从服务器获取 UTC 日期
"endValidityDate": "2021-11-18T22:59:59Z"
Run Code Online (Sandbox Code Playgroud)
我想知道计算从现在起剩余天数的最佳方法是什么。
这是我现在得到的:
我正在创建一个 2天后的日期:
DateTime.now().plusSeconds(172800)
Run Code Online (Sandbox Code Playgroud)
我正在将其解析为DateTimejoda,如果您这么说,我可以使用其他内容。
当做不同的日子时,我这样做
val diff = endValidityDate.toDate().time - Date().time
val daysRemaining = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)
return if (daysRemaining > 1) "$daysRemaining days}"
else TimeUnit.DAYS.convert(diff, TimeUnit.SECONDS).toString()
Run Code Online (Sandbox Code Playgroud)
我想要实现的场景是:
如果剩余天数超过 1(24 小时),则打印“剩余 2 天”,而不是显示“剩余 1 天”,然后只需添加一个计时器,如下所示:
“0小时43米3秒”。
为了做计时器,我只需减去现在剩余的时间
val expireDate = LocalDateTime.now()
.plusSeconds(uiState.endValidityDate.timeLeft.toLong())
.toEpochSecond(ZoneOffset.UTC)
val currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
Run Code Online (Sandbox Code Playgroud)
然后每一秒发生的时候我都会像这样打印它:
val duration = Duration.ofSeconds(it)
binding.myTextView.text = String.format(
"%02dh: %02dm: %02ds",
duration.seconds / 3600,
(duration.seconds % 3600) / 60,
duration.seconds % …Run Code Online (Sandbox Code Playgroud) 我正在使用 a ,@Composable我需要通过参数 an 传递ImageBitmap,问题是我从给定 url 的服务器获取图像,所以我需要加载这些图像,将它们转换为 a Bitmap,然后转换为 aImageBitmap但我很卡住因为我不知道如何将其转换为 an ImageBitmap,这是我的@Composable
@ExperimentalComposeUiApi
@Composable
fun MyCanvas(
myImage: ImageBitmap,
modifier: Modifier = Modifier,
) {
Canvas(modifier = modifier
.size(220.dp)
.clipToBounds()
.clip(RoundedCornerShape(size = 16.dp)) {
...
val canvasWidth = size.width.toInt()
val canvasHeight = size.height.toInt()
val imageSize = IntSize(width = canvasWidth, height = canvasHeight)
drawImage(
image = myImage, dstSize = imageSize
)
...
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我调用此方法时,@Composable我需要加载图像,但不确定如何开始,并且我需要知道使用 Glide 或 Coil 哪个更好。
我有一个Composable,LazyColumn但我正在尝试检查LazyListState我曾经使用过的可见性
fun LazyListState.visibleItems(itemVisiblePercentThreshold: Float) =
layoutInfo
.visibleItemsInfo
.filter {
visibilityPercent(it) >= itemVisiblePercentThreshold
}
private fun LazyListState.visibilityPercent(info: LazyListItemInfo): Float {
val cutTop = maxOf(0, layoutInfo.viewportStartOffset - info.offset)
val cutBottom = maxOf(0, info.offset + info.size - layoutInfo.viewportEndOffset)
return maxOf(0f, 100f - (cutTop + cutBottom) * 100f / info.size)
}
Run Code Online (Sandbox Code Playgroud)
但现在我想要它在里面,Composable因为我无权访问列表,其他功能使用我的Composables,它可能在 , 等里面LazyList,Surface所以我用它onGloballyPositioned来确定它是否可见,但我想了解是否至少有 30% 可见。任何想法?
我想为我的应用程序的其他功能提供一个@Composable,例如:
Feature1 有一个LazyColumn打印它的项目,但是他们想将 my 添加@Composable到列表的顶部,所以我想知道 …
我创建了一个Toolbar并将其添加到我的 main_activity.xml 中,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/loginBrown"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="@color/nav_item_state_list"
app:itemTextColor="@color/nav_item_state_list"
app:menu="@menu/bottom_navigation_items" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
还更改style.xml为:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
我添加MainActivity了这个:
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar_detail);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)
但即使这样也没有在第一次出现Fragment
我做了一个fragment.replace()来显示第一个Fragment,我不知道这是不是错误的事情
我知道如何在知道键值的情况下解析 JSON,但现在我想从不是我的 JSON 中获取键值,所以我可以知道键名,例如我有这个 JSON
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": …Run Code Online (Sandbox Code Playgroud)