小编Gil*_*eig的帖子

kotlin中的全局扩展功能

嘿,我想在kotlin中创建一个类,它将包含我将在几个地方使用的所有扩展函数,例如:

class DateUtils {
    //in this case I use jodatime
    fun Long.toDateTime() : DateTime = DateTime(this)
    fun String.toDateTime() : DateTime = DateTime.parse(this)
}


class SomeClassWithNoConnectionToDateUtils {
    fun handleDataFromServer(startDate: String) {
        someOtherFunction()
        //startDate knows about toDateTime function in DateUtils 
        startDate.toDateTime().plusDays(4)
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法执行此类操作

kotlin extension-function

12
推荐指数
1
解决办法
3944
查看次数

kotlin注释处理器中的可空类型

我正在为Kotlin开发注释处理器,因为处理过的元素是用Java编写的,我没有收到?带有@Nullable注释的nullables ,这很好,但是我遇到了在类型和高阶函数中接收空参数的问题,对于正常参数.

var someNullField: String? = "" 
Run Code Online (Sandbox Code Playgroud)

我将在其注释java.lang.String过程@org.jetbrains.annotations.Nullable中收到.

但是List<String?>例如将返回我java.util.List<java.lang.String>没有任何注释不在主元素中而不在类型参数中导致未知的可空性状态

我尝试使用javax.lang.model.util.Types找到某种结果,但没有.

我现在使用的一些代码:

val utils = processingEnvironment.typeUtils
val type = fieldElement.asType()
if (type is DeclaredType) {
    val typeElement = utils.asElement(type)
    type.typeArguments
            .forEach {
                //Trying different ways and just printing for possible results
                val capture = utils.capture(it)
                val erasure = utils.erasure(it)
                val element = utils.asElement(it)
                printMessage("element: $element isNullable: ${element.isNullable()} isNotNull: ${element.isNotNull()}\ncapture: $capture isNullable: ${capture.isNullable()} isNotNull: ${capture.isNotNull()}\nerasure: $erasure isNullable: ${erasure.isNullable()} …
Run Code Online (Sandbox Code Playgroud)

java annotations annotation-processing kotlin kapt

8
推荐指数
1
解决办法
659
查看次数

如何使用 MotionLayout 调整 TextView 的大小

我正在尝试CollapsingToolbar使用MotionLayout.

我已经成功地将所有内容动画化为CollapsingToolbar具有高度灵活性的行为,这意味着我可以轻松创建出色的动画而无需编写大量代码。

我的问题是无论我尝试过什么;我无法TextView以自然的方式调整标题的大小。

我目前使用的ConstraintLayout版本2.0.0-beta3

试验#1

CustomAttributetextSize

<ConstraintSet android:id="@+id/dish_fragment_expanded_set">

    ...

    <Constraint
        android:id="@+id/dish_fragment_title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dish_fragment_cover_image">

        <CustomAttribute
            app:attributeName="textSize"
            app:customFloatValue="24" />

    </Constraint>

    ...

</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)
<ConstraintSet android:id="@+id/dish_fragment_collapsed_set">

    ...

    <Constraint
        android:id="@id/dish_fragment_title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        app:layout_constraintBottom_toBottomOf="@+id/dish_fragment_navigation_icon"
        app:layout_constraintStart_toEndOf="@+id/dish_fragment_navigation_icon"
        app:layout_constraintTop_toTopOf="@id/dish_fragment_navigation_icon">

        <CustomAttribute
            app:attributeName="textSize"
            app:customFloatValue="16" />

    </Constraint>

    ...

</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)

结果

上述解决方案有效,但文本在移动时闪烁,这意味着动画不流畅。


试验#2

scaleX & scaleY

<ConstraintSet android:id="@+id/dish_fragment_expanded_set">

    ...

    <Constraint
        android:id="@+id/dish_fragment_title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dish_fragment_cover_image"
        android:scaleX="1"
        android:scaleY="1"/>

    ...

</ConstraintSet>
Run Code Online (Sandbox Code Playgroud)
<ConstraintSet android:id="@+id/dish_fragment_collapsed_set"> …
Run Code Online (Sandbox Code Playgroud)

android android-collapsingtoolbarlayout android-motionlayout

7
推荐指数
1
解决办法
2168
查看次数

更改kotlin扩展函数接收器JVM名称

这是一个普遍的问题.假设我有一个用kotlin编写的扩展函数,它将DP转换为PX并返回一个NonNull Int

fun Int.toPx() {  /** implementation */ }
Run Code Online (Sandbox Code Playgroud)

java中的函数看起来像这样

public int toPx(int $receiver) {  /** implementation */ }
Run Code Online (Sandbox Code Playgroud)

在我看来,这$receiver使得Java-interop感觉生成和不受欢迎.

我知道你可以使用@JvmName注释和一些组合@file:JvmName来改变java中的名字.

当我尝试使用@JvmNamereceiver网站的目标,它说

"此注释不适用于目标type usage和使用站点目标@receiver"

有没有办法克服这一点并改变接收器的名称,如果不是最好的选择.

java java-interop kotlin kotlin-interop

6
推荐指数
1
解决办法
652
查看次数

Resource IDs will be non final in AGP 7.0

I have an annotation processing library that generates RecyclerView adapters in compile time.

I'm currently rebuilding it from the ground up with many changes and improvements, but while testing, I received a warning stating:

Resource IDs will be non-final in Android Gradle Plugin version 7.0, avoid using them as annotation attributes

This is a problem because it means I won't be able to use R.layout variables in annotations.

I currently use it to associate the layout file's integer value with …

android annotation-processing android-gradle-plugin

5
推荐指数
1
解决办法
334
查看次数