小编fik*_*r4n的帖子

Python:检查对象是否是序列

在python中有一个简单的方法来判断某些东西是不是一个序列?我试着这么做: if x is not sequence但是python并不喜欢这样

python if-statement sequences sequence

58
推荐指数
6
解决办法
3万
查看次数

为什么要使用双冒号(::)在Kotlin上课?

我们知道,双冒号(::)用于获取在科特林,如功能(可赎回)的参考String::compareTo,"string"::compareTo.

在Java中,我们使用SomeClass.classsomeInstance.getClass()获取类.为什么在Kotlin我们使用SomeClass::classsomeInstance::classwhile class不是函数/方法?

println(String::compareTo)
// output: fun kotlin.String.compareTo(kotlin.String): kotlin.Int
println("string".compareTo("strong"))
// output: -6
println(String::class)
// output: class kotlin.String
println("string".class)
// compile error
Run Code Online (Sandbox Code Playgroud)

java operators kotlin

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

为什么在Python中使用正则表达式搜索"不完全等同于切割字符串"?

正如文档所述,使用regex.search(string, pos, endpos)并不完全等同于切割字符串,即regex.search(string[pos:endpos]).它不会字符串开始那样进行正则表达式匹配pos,因此^不匹配子字符串的开头,而只匹配整个字符串的实际开头.但是,$匹配子字符串的结尾或整个字符串.

    >>> re.compile('^am').findall('I am falling in code', 2, 12)
    []        # am is not at the beginning
    >>> re.compile('^am').findall('I am falling in code'[2:12])
    ['am']    # am is the beginning
    >>> re.compile('ing$').findall('I am falling in code', 2, 12)
    ['ing']   # ing is the ending
    >>> re.compile('ing$').findall('I am falling in code'[2:12])
    ['ing']   # ing is the ending

    >>> re.compile('(?<= )am').findall('I am falling …
Run Code Online (Sandbox Code Playgroud)

python regex string substring

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

如何延长Dia中序列图的生命线?

我想通过使用Dia中的UML图表来制作序列图.我可以修改或延长生命线的矩形吗?当我扩展它时,只有延伸的虚线.

uml sequence-diagram dia

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

如何解决Kotlin中有限束限制的违反?

假设我在Java中有这个声明,没关系.

abstract class Start<T extends End> {
    public T end;
}

abstract class End<T extends Start> {
    public T start;
}
Run Code Online (Sandbox Code Playgroud)

然而,在Kotlin中它并不合适,因为Kotlin对"循环"类型参数有限制.

abstract class Start<T : End<*>> {
    lateinit var end: T
}

abstract class End<T : Start<*>> {
    lateinit var start: T
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以在Kotlin中解决这个问题,这样我可以拥有相互依赖的泛型类型吗?

generics kotlin

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

动态功能模块中的 ConstraintLayout Barrier 失败

我有ConstraintLayout一个XML布局中,它包含了3次和Barrier,它们是button2textView2barrier2,和button3。正如预期的那样,button3成功地放置在button2和 下textView2,受使用 约束barrier2。然而,当在动态特征模块中使用时,它似乎没有引用约束视图(button2textView2),所以它坚持顶部。button3

这些屏幕截图显示它是成功的基础模块,但在动态功能模块中不起作用:

截图

基本功能动态功能中的 XML 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 2"
        app:layout_constraintStart_toEndOf="@id/button2"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="button2,textView2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier2" …
Run Code Online (Sandbox Code Playgroud)

android android-constraintlayout android-app-bundle

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

如何在Java注释处理中获取字段的类型注释?

例如,我有以下代码:

@Retention(RetentionPolicy.SOURCE)
public @interface ClassAnnotation {
}

@ClassAnnotation
public class AnnotatedClass {
}

@ClassAnnotation
public class AnotherAnnotatedClass {

    private AnnotatedClass someField;
    private int intIsNotAnnotated;
}
Run Code Online (Sandbox Code Playgroud)

并在编译时对该处理器进行预处理:

@SupportedAnnotationTypes({ "com.example.ClassAnnotation" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AwesomeProcessor extends AbstractProcessor {

    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {
        // Skipped for brevity...
        // For each annotated class
        for (Element e : roundEnv.getElementsAnnotatedWith(ClassAnnotation.class)) {
            // Skipped for brevity...
            // For each field
            for (Element ee : classElement.getEnclosedElements()) {
                // Skipped for brevity... (of course …
Run Code Online (Sandbox Code Playgroud)

java preprocessor annotations annotation-processing

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

使用GridLayoutManager滚动RecyclerView时出现ArrayIndexOutOfBoundsException

我正在使用RecyclerView(com.android.support:recyclerview-v7:21.0.2)GridLayoutManager并应用动态列数。滚动视图时,将引发异常。当RecyclerView即将显示以前未显示的项目(重新使用或重新绑定)时,似乎发生了错误。但是,如果我不使用这样的动态跨度/列数,或者只是注释行调用GridLayoutManager.setSpanCount,则不会发生。

我试图检查源代码,看来问题与临时数组有关mSet

E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.ArrayIndexOutOfBoundsException
            at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:361)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
            at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1097)
            at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:957)
            at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:985)
            at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:1686)
            at android.view.View.dispatchTouchEvent(View.java:3885)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
            at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) …
Run Code Online (Sandbox Code Playgroud)

android gridlayoutmanager android-recyclerview

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

ValueAnimator是否可以避免内存泄漏?

我做了一个无限的ValueAnimator,它持有对视图的引用Context(当然视图保持引用),并且日志显示它在视图被(应该被)销毁时不会停止.

    ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
    anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationRepeat(Animator animation) {
                myView.setText(...);
                Log.d("my_tag", "I am still running and repeating");
            }
    });
    anim.setRepeatCount(ValueAnimator.INFINITE);
    anim.start();
Run Code Online (Sandbox Code Playgroud)

当然,我可以通过将侦听器提取为静态内部类并保持一个WeakReference而不是保持(强)引用.但是,我的问题是,如果有限的动画师(例如anim.setRepeatCount(3))在定义的时间内停止,它会在动画结束后导致内存泄漏吗?(所以我需要做同样的弱参考策略)?

android memory-leaks android-animation

5
推荐指数
0
解决办法
508
查看次数

Android:自定义键盘行不居中

我正在尝试为Android创建自定义键盘.我使用软键盘示例应用程序作为起点.我的问题是我无法在键盘中居中,而我在网上找到的其他样本显然有效.

我的XML文件如下所示:

<?xml version="1.0" encoding="utf-8"?><Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="@dimen/horizontal_gap"
android:keyHeight="@dimen/key_height"
android:keyWidth="10%p"
android:verticalGap="@dimen/vertical_gap" >
<Row>
    <Key
        android:codes="49"
        android:keyEdgeFlags="left"
        android:keyLabel="1" />
    <Key
        android:codes="50"
        android:keyLabel="2" />
    <Key
        android:codes="51"
        android:keyLabel="3" />
    <Key
        android:codes="52"
        android:keyLabel="4" />
    <Key
        android:codes="53"
        android:keyLabel="5" />
    <Key
        android:codes="54"
        android:keyLabel="6" />
    <Key
        android:codes="55"
        android:keyLabel="7" />
    <Key
        android:codes="56"
        android:keyLabel="8" />
    <Key
        android:codes="57"
        android:keyLabel="9" />
    <Key
        android:codes="48"
        android:keyEdgeFlags="right"
        android:keyLabel="0" />
</Row>
<Row>
    <Key
        android:codes="1"            
        android:keyEdgeFlags="left"
        android:keyLabel="q" />
    <Key
        android:codes="2"
        android:keyLabel="w" />
    <Key
        android:codes="3"
        android:keyLabel="e" />
    <Key
        android:codes="4"
        android:keyLabel="r" />
    <Key
        android:codes="5"
        android:keyLabel="t" />
    <Key
        android:codes="6"
        android:keyLabel="y" …
Run Code Online (Sandbox Code Playgroud)

keyboard android center ime

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