如何在AndroidX中实现Butterknife.Action?

问题描述 投票:0回答:4

我无法在 androidx 中使用 ButterKnife.Action,有人可以提供有关如何在 androidx 中使用 ButterKnife.Action 的建议吗?

类路径'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2'

实现“com.jakewharton:butterknife:10.0.0” 注释处理器'com.jakewharton:butterknife-compiler:10.0.0'

androidx butterknife
4个回答
0
投票

如果您迁移到 AndroidX,则必须使用:

在 gradle 中

依赖项{

实现“com.jakewharton:butterknife:10.2.0”

annotationProcessor“com.jakewharton:butterknife-compiler:10.2.0”

...}

课堂上出现错误时

如果你使用这样的代码:

  • ButterKnife.apply(new View[]{ ivImageId,vgSubscribtionsId,vgSubscribersID},

                (view, value, index) -> view.setVisibility(value), View.INVISIBLE);
    

替换为与 AndroidX 配合使用:

  • butterknife.Action viewAction = (视图, 索引) -> {

            view.setVisibility(View.INVISIBLE);
    
        };
        butterknife.ViewCollections.run(new View[]{ ivImageId,vgSubscribtionsId,vgSubscribersID} , viewAction );
    

0
投票

我使用的是ButterKnife 10.2.1版本。 按照以下步骤操作,问题将得到解决。

//第1步:导入此行

import butterknife.Action;

//第 2 步:在范围类中

butterknife.Action viewAction = new Action() 
{

     @Override public void apply(@NonNull View view, int index) 
    {
    // do something.
    }

};

//第3步:新版本中的ButterKnife.apply()已弃用。替换为:

 butterknife.ViewCollections.run(listView , viewAction );

0
投票
In Android studio Flamingo and above version below code working for butterknife library.

 1. add below code in build.gradle(:app) file 

    tasks.withType(JavaCompile).configureEach {
        options.fork = true
        options.forkOptions.jvmArgs += [
                '--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
                '--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED',
        ]
    }

 2. 
    android {
      ...
      // Butterknife requires Java 8.
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
      }
    }
    
    dependencies {
      implementation 'com.jakewharton:butterknife:10.2.3'
      annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
    }
    
     3. To use Butter Knife in a library, add the plugin to your buildscript:
    
    buildscript {
      repositories {
        mavenCentral()
        google()
      }
      dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
      }
    }
    
     4. and then apply it in your module:
    apply plugin: 'com.android.library'
    apply plugin: 'com.jakewharton.butterknife'
    
     5. Now make sure you use R2 instead of R inside all Butter Knife annotations.
    class ExampleActivity extends Activity {
      @BindView(R2.id.user) EditText username;
      @BindView(R2.id.pass) EditText password;
    ...
    }

-1
投票

有了

androidX
,你应该使用

import butterknife.ButterKnife;

然后做一些新的事情

butterknife.Action<View>()

包名称现在是

butterknife
,带有
'b'
,而之前的 if 带有
'B'

我希望这会有所帮助...

© www.soinside.com 2019 - 2024. All rights reserved.