小编All*_*n W的帖子

什么".()"在Kotlin中意味着什么?

我见过一个函数有一个由ClassName给出的参数的例子.()这似乎不是一个扩展函数,它是ClassName.Function()

一个例子是Kotterknife:

private val View.viewFinder: View.(Int) -> View?
    get() = { findViewById(it) }
Run Code Online (Sandbox Code Playgroud)

其中我不太了解其功能,

MaterialDrawerKt

fun Activity.drawer(setup: DrawerBuilderKt.() -> Unit = {}): Drawer {
    val builder = DrawerBuilderKt(this)
    builder.setup()
    return builder.build()
}
Run Code Online (Sandbox Code Playgroud)

代码允许您直接调用的地方

drawer {
    ...
}
Run Code Online (Sandbox Code Playgroud)

而不是给括号括起来的参数.

在任何地方都有这方面的文件吗?

android object higher-order-functions kotlin navigation-drawer

15
推荐指数
4
解决办法
2157
查看次数

项目不会与Kotlin 1.1.3一起构建

我有一个Android项目,我最近用1.1.3更新,每次构建时都会出现以下错误:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> kotlin.jvm.internal.FunctionReference.<init>(ILjava/lang/Object;)V
Run Code Online (Sandbox Code Playgroud)

其他问题通常说这意味着stdlib不包括在内,但我肯定已添加它.1.1.2-5虽然使用作品,并1.1.3已为其他项目工作.

问题在于我将注释处理器与kapt混合在一起.对于像DbFlow和Glide这样的项目,annotationProcessor依赖项可以直接与kapt一起使用.

回到原来的问题:

是我的Travis日志,下面是我的依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.3-alpha', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    compile("ca.allanwang:kau:${KAU}")

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:${KOTLIN}"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:${KOTLIN}"

    debugCompile "com.squareup.leakcanary:leakcanary-android:${LEAK_CANARY}"
    releaseTestCompile "com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}"
    releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}"
    testCompile "com.squareup.leakcanary:leakcanary-android-no-op:${LEAK_CANARY}"

    compile "com.github.Raizlabs.DBFlow:dbflow:${DBFLOW}"
    compile "com.github.Raizlabs.DBFlow:dbflow-core:${DBFLOW}"
    annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${DBFLOW}"
    kapt "com.github.Raizlabs.DBFlow:dbflow-processor:${DBFLOW}"
    compile "com.github.Raizlabs.DBFlow:dbflow-kotlinextensions:${DBFLOW}"

    //Icons
    compile "com.mikepenz:material-design-iconic-typeface:${IICON_MATERIAL}@aar"
    compile "com.mikepenz:community-material-typeface:${IICON_COMMUNITY}@aar"

    compile "org.jsoup:jsoup:${JSOUP}"

    compile "com.github.bumptech.glide:glide:${GLIDE}"
    annotationProcessor "com.github.bumptech.glide:compiler:${GLIDE}"

    compile("com.mikepenz:materialdrawer:${MATERIAL_DRAWER}@aar") {
        transitive = true
    }
    compile "co.zsmb:materialdrawer-kt:${MATERIAL_DRAWER_KT}"

    compile "nz.bradcampbell:paperparcel:${PAPER_PARCEL}"
    compile …
Run Code Online (Sandbox Code Playgroud)

android kotlin

14
推荐指数
1
解决办法
2374
查看次数

在Kotlin可以进行交叉铸造吗?

我有一个Java方法,如下所示:

public <T extends A & B> methodName(T arg, ...)
Run Code Online (Sandbox Code Playgroud)

其中A是类,B是接口.

在我的kotlin类中,我有另一个variable类型C,我希望实现以下目标:

if (variable is A && variable is B) {
    methodName(variable, ...)
} else {
    // do something else
}
Run Code Online (Sandbox Code Playgroud)

是否可以正确投射,variable以便它可以作为参数使用而不会出错?

目前,variable有一个setter方法,因此没有智能铸造.但是,我还使用本地测试了它,val并且推断出值具有Any无效的类型.

casting type-inference kotlin

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

将ConstraintLayout项目对齐到两个项目的末尾

我有一个ConstraintLayout,有两个垂直堆叠的视图A和B. 我有第三个视图C,它需要水平地到达A和B的末尾.在任何给定点,A可以比B宽,反之亦然,因此约束不仅可以基于一个视图.有没有办法通过视图C定义这个约束?

目前,我可以定义A和B.

app:layout_constraintEnd_toStartOf="C"
Run Code Online (Sandbox Code Playgroud)

这确实有效,但由于C中没有启动约束,设计预览将无法正确绘制其他属性,例如

app:layout_constraintHorizontal_bias="1.0"
Run Code Online (Sandbox Code Playgroud)

另一种选择可能是以某种方式将A组和B组分组.大多数关于分组的问题谈论链,我认为这不能解决这个问题.添加另一个视图来包装这两个也似乎打败了ConstraintLayout的目的,这意味着消除嵌套的子节点.

编辑:我在下面附上了一个例子:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/view_c"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:text="View C"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <TextView
        android:id="@+id/view_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="View A"
        app:layout_constraintBottom_toTopOf="@id/view_b"
        app:layout_constraintEnd_toStartOf="@id/view_c"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginBottom="16dp" />

    <TextView
        android:id="@+id/view_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="View B"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view_c"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_a" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,预览应该在中间某处显示"View C",因为它的偏差是0.5.但是,它不知道它的起始界限,因为它们在view_a和view_b中定义,因此坚持正确.

解:

以下是我的最终布局

<?xml version="1.0" encoding="utf-8"?>

<!--due to animations, we need a wrapper viewgroup so our changes will stick-->

<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-constraintlayout

10
推荐指数
1
解决办法
8379
查看次数

DataKind工会

我不确定它是否是正确的术语,但是可以声明接受datakinds"联合"的函数类型吗?

例如,我知道我可以执行以下操作:

{-# LANGUAGE DataKinds      #-}
{-# LANGUAGE GADTs          #-}

...

data Shape'
  = Circle'
  | Square'
  | Triangle'

data Shape :: Shape' -> * where
  Circle :: { radius :: Int} -> Shape Circle'
  Square :: { side :: Int} -> Shape Square'
  Triangle
    :: { a :: Int
       , b :: Int
       , c :: Int}
    -> Shape Triangle'

test1 :: Shape Circle' -> Int
test1 = undefined
Run Code Online (Sandbox Code Playgroud)

但是,如果我想要采用圆形或正方形的形状呢?如果我还想将所有形状用于单独的功能怎么办?

有没有办法让我定义一组Shape'要使用的种类,或者让我允许每个数据允许多个datakind定义?

编辑:

工会的使用似乎不起作用:

{-# LANGUAGE ConstraintKinds #-} …
Run Code Online (Sandbox Code Playgroud)

haskell data-kinds

8
推荐指数
2
解决办法
216
查看次数

在APK META-INF/library_release.kotlin_module中复制的重复文件

我最近通过JitPack添加了两个Android库,我有以下错误:

Duplicate files copied in APK META-INF/library_release.kotlin_module
Run Code Online (Sandbox Code Playgroud)

我已经清除了缓存,我尝试将模块排除在外

exclude group: 'org.jetbrains'
Run Code Online (Sandbox Code Playgroud)

exclude group: 'org.jetbrains.kotlin'
Run Code Online (Sandbox Code Playgroud)

但似乎都没有解决这个问题.有没有办法阻止kotlin stdlib通过JitPack添加?奇怪的是,像DbFlow这样的其他库没有这个问题,虽然我没有看到他们的设置有什么特别之处(除了它不是通过JitPack)

android conflicting-libraries kotlin

7
推荐指数
3
解决办法
4501
查看次数

Data Binding inflation is very slow

I'm currently using DataBinding with a RecyclerView, and I'm getting pretty severe lag when a list first loads. However, after I scroll past a page and it stops creating new viewholders, everything is fine.

During creation, the only thing I'm doing is inflating a layout using DataBindingUtils, so I'm wondering if there are parts that can be improved.

Attached below are relevant code snippets. I'm currently using a library, FastAdapter, so the signatures will not match exactly

When …

android kotlin android-recyclerview android-databinding

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

从记录中的字段派生实例

示例代码:

{-# LANGUAGE NamedFieldPuns #-}

module Sample where

class Sample a where
  isA :: a -> Bool
  isB :: a -> Bool
  isC :: a -> Bool

data X =
  X

instance Sample X where
  isA = undefined
  isB = undefined
  isC = undefined

data Wrapper = Wrapper
  { x :: X
  , i :: Int
  }

instance Sample Wrapper where
  isA Wrapper {x} = isA x
  isB Wrapper {x} = isB x
  isC Wrapper {x} = isC x
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个由实现的类, …

haskell deriving

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

Android Studio 包未正确嵌套

这件事发生得太突然了,所以我不知道发生了什么。

在此输入图像描述

所有的包都单独显示。

我没有更改任何项目设置,并且尝试了以下操作:

  • 缓存无效。
  • 更新了 android studio(从 beta 2 到 beta 7,完全独立)。

如果我尝试在某处创建一个新的子包,它将创建一个新的未嵌套列表,如图所示。

这会影响我从同一 AS 版本打开的所有项目。
我的 Stable 2.3.0 版本打开一切正常。

任何帮助将不胜感激。

intellij-idea android-studio android-studio-3.0

4
推荐指数
1
解决办法
445
查看次数

处置可观察量

这个问题与Android和生命周期有关。以前,我会有一系列主题并在创作时订阅它们。

销毁后,我会将所有主题标记为完整,假设它处理了所有订阅者。

在 Android Studio 3.1 中,我会收到针对“未使用”的任何订阅者的警告。解决方案是将它们添加到“完全一次性”中,然后我在销毁时进行处理。

我需要“复合一次性”来在销毁时正确取消请求吗?我之前将主题标记为完整的方法有什么作用吗?在这种情况下有必要吗?

作为代码示例:

val observable: PublishSubject<Int> = PublishSubject.create()
val disposable = observable.subscribe { /* subscription */ }

fun onDestroy() {
    observable.onComplete() // is this line necessary or helpful?
    disposable.dispose()
}
Run Code Online (Sandbox Code Playgroud)

android rx-java2 rx-kotlin2

2
推荐指数
1
解决办法
5647
查看次数

嵌套不明确类型的方法

说我有一堂课

class T where
    tag1 :: String
    tag2 :: String
Run Code Online (Sandbox Code Playgroud)

启用模糊类型后,我可以在实例中指定每个类型:

instance T A where
    tag1 = "tag1"
    tag2 = "tag2"
Run Code Online (Sandbox Code Playgroud)

如果我想对tag2追加内容tag1,则可以定义

instance T A where 
    tag1 = "tag1"
    tag2 = tag1 @A ++ " suffix"
Run Code Online (Sandbox Code Playgroud)

这很好用,但是如果我想tag2始终附加suffixtag1每个实例,则似乎必须为每个实例指定模糊的调用。

我了解此需求,因为tag1从任何情况下,每个呼叫都可以使用。但是,haskell中是否有任何技巧可以让我仅指定一次?

就像是

tag2 :: T a => String
tag2 = tag1 @a ++ " suffix"
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
2
解决办法
66
查看次数

Show的重叠实例

假设我们有以下内容:

{-# LANGUAGE FlexibleInstances #-}

module Sample where

newtype A a =
  A a
  deriving (Show)

newtype L a =
  L [a]

class ListContainer l where
  getList :: l a -> [a]

instance ListContainer L where
  getList (L l) = l

instance (Show a, ListContainer l) => Show (l a) where
  show = const "example"
Run Code Online (Sandbox Code Playgroud)

使用此代码,ghc 抱怨:

warning: [-Wdeferred-type-errors]
• Overlapping instances for Show (A a)
    arising from a use of ‘GHC.Show.$dmshowList’
  Matching instances:
    instance (Show a, ListContainer l) => …
Run Code Online (Sandbox Code Playgroud)

haskell overlapping-instances

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