【问题标题】:How to Implement Butterknife.Action in AndroidX?如何在 AndroidX 中实现 Butterknife.Action?
【发布时间】:2019-06-25 12:47:02
【问题描述】:

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

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

实现 'com.jakewharton:butterknife:10.0.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'

【问题讨论】:

  • 使用这个无用的库有什么意义?如果您使用 Kotlin,则不再需要 findVieById() 并且分配点击侦听器更加方便和有用

标签: androidx butterknife


【解决方案1】:

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

//第一步:导入这一行

import butterknife.Action;

//第二步:在作用域类中

butterknife.Action viewAction = new Action() 
{

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

};

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

 butterknife.ViewCollections.run(listView , viewAction );

【讨论】:

    【解决方案2】:

    如果您迁移到 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, index) -> {

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

    【讨论】:

      【解决方案3】:

      对于androidX,你应该使用

      import butterknife.ButterKnife;
      

      然后做类似新的butterknife.Action<View>()

      包名现在是 butterknife'b' 而之前的 if 是 'B'

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

      【讨论】:

      • 这不是正确的解释,以前的包名也是一样的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多