1.BR找不到,无法自动更新

1.1 描述

  https://stackoverflow.com/questions/57983508/android-studio-kotlin-databinding-unresolved-reference-on-br

  • I have some code that is generating a "red squiggly" error in Android Studio:

     @get:Bindable
    var title: String = ""
    set(value) {
    field = value
    notifyPropertyChanged(BR.title)
    }

    It complains that "title" is an unresolved reference on BR.title. Building and running works fine though, and this is the only error I can see. I debug there and see that it's gotten the value for BR.title correctly.

    Still, I can't figure out how to make it go away. I verified that the generated BR class has the "title" field, but Android Studio refuses to recognize this. I've looked up people having this issue and have tried the following: (unsuccessfully)

    • Closing Android Studio, deleting the .gradle, .idea and build folders and restarting
    • Build -> Clean Project, Rebuild Project
    • File -> Invalidate Caches and restart
    • Disabling and enabling the Kotlin plugin
    • Closing and reopening the project

    I have also checked and I have apply plugin: 'kotlin-kapt' in build.gradle.

1.2 解决方法

 apply plugin: 'com.android.application'

 apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions' android { dataBinding {
enabled = true
} } dependencies { ...
kapt 'com.android.databinding:compiler:3.2.0-alpha10'
}
  • apply plugin: 'kotlin-kapt'
  • kapt 'com.android.databinding:compiler:3.2.0-alpha10'

2. 闪烁bug

item使用数据绑定时,当复用ViewHolder时,会有闪烁问题,或者排序问题,

复现方法:

  1. 关闭RecyclerView item的动画,
  2. 多准备条数据,超过1屏,上下滚动,长按item进入编辑状态,选中item,就会出现。

3.不自动更新

3.1 问题描述

  kotlin与Java 混合时, kotlin修改java里的data ,不自动更新

  • 定义类

     public class Data extends BaseObservable {
    
         public int      icon    ;
    public String key ;
    public int value ;
    public String test = ""; @Bindable public String getKey() { return key;}
    @Bindable public int getValue() { return value; }
    @Bindable public int getIcon() {
    return icon;
    }
    @Bindable public String getTest(){
    return test;
    } public void setKey(String key) {
    this.key = key;
    notifyPropertyChanged(BR.key);
    } public void setValue(int value) {
    this.value = value;
    notifyPropertyChanged(BR.value);
    }
    public void setIcon(int icon) {
    this.icon = icon;
    notifyPropertyChanged(BR.icon);
    }
    public void setTest(String ss){
    this.test = ss;
    notifyPropertyChanged(BR.test);
    } @Override
    public String toString() {
    return "key = " + key + " value = " + value;
    }
    }
  • 布局中

             <TextView
    android:id="@+id/data_key"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:text='@{data.key}'
    app:layout_constraintStart_toStartOf="@+id/data_title"
    app:layout_constraintTop_toBottomOf="@+id/data_title" /> <TextView
    android:id="@+id/data_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:text='@{String.valueOf(data.value)}'
    app:layout_constraintStart_toEndOf="@+id/data_key"
    app:layout_constraintTop_toTopOf="@+id/data_key" />
  • 修改data值

         fun onDataThreadMainClicked(view: View){
    val random = (Math.random() * ).toInt()
    data.key = "新Main key$random"
    data.value = random
    binding.data = data
    }

  结果:界面不刷新

3.2 解决办法

      fun onDataThreadMainClicked(view: View){
val random = (Math.random() * ).toInt()
data.key = "新Main key$random"
data.value = random
binding.data = data
}

或者

     fun onDataThreadMainClicked(view: View){
val random = (Math.random() * ).toInt()
data.key = "新Main key$random"
data.value = random
binding.invalidateAll()
}

android 数据绑定(5) kotlin 的binding bug的更多相关文章

  1. Android数据绑定技术一,企业级开发

    PS:数据绑定,顾名思义是数据与一些控件或者用户账号等绑定,这样用的好处是便于管理.代码清晰,量少. 首先要了解什么是数据绑定? 为什么要用数据绑定? 怎么用数据绑定? 语法的使用 简单例子,数据绑定 ...

  2. 写给Android开发者的Kotlin入门

    写给Android开发者的Kotlin入门 转 https://www.jianshu.com/p/bb53cba6c8f4 Google在今年的IO大会上宣布,将Android开发的官方语言更换为K ...

  3. Android数据绑定DataBinding(二)入门篇

    前言 之前写了Android数据绑定DataBinding(一)入门篇,很简单的记录了如何使用DataBinding,其初衷是想要代码中的数据发生改变,不需要繁琐的setText等操作,在最后说到了只 ...

  4. Android开发者的Kotlin:书

    原文标题:Kotlin for Android Developers: The book 原文链接:http://antonioleiva.com/kotlin-android-developers/ ...

  5. [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found

    google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...

  6. android一个下拉放大库bug的解决过程及思考

    android一个下拉放大库bug的解决过程及思考 起因 项目中要做一个下拉缩放图片的效果,搜索了下github上面,找到了两个方案. https://github.com/Frank-Zhu/Pul ...

  7. 数据绑定(九)Binding的数据校验

    原文:数据绑定(九)Binding的数据校验 Binding用ValidationRules属性来校验数据的有效性,ValidationRules属性类型是Collection<Validati ...

  8. 数据绑定(十)Binding的数据转换

    原文:数据绑定(十)Binding的数据转换 当Source端Path所关联的数据与Target端目标属性数据类型不一致时,需要添加数据转换器,数据转换器是一个自定义的类,这个类需要实现IValueC ...

  9. Android开发教程 - 使用Data Binding(五)数据绑定

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

随机推荐

  1. 回首Java——写在前面

    我记得在大学的课程要求中,第一个接触的高级编程语言,应该是C语言或者C++等.但是Java应该是我的编程母语,我在高中毕业就接触了Java语言.当时看的是纸质书,具体书名也忘记了.只记得当时第一次接触 ...

  2. MVVM 框架

    问题: 1.MVVM 的定义 M (Model): 数据来源,服务器上业务逻辑操作 V (View): 界面,页面 VM (ViewModel): view 和 model 的核心枢纽,如 vue.j ...

  3. 吴太银:华为消费者云服务Cassandra使用场景与最佳实践

    大家好,我是华为消费者云的吴太银. 我今天分享的主要是华为消费者云服务使用Cassandra的应用场景和最佳实践.我这个可能跟其他嘉宾分享的不太一样,因为前几个嘉宾讲的实际上对Cassandra原生的 ...

  4. 移动物体监控系统-sprint3移动监控主系统设计与开发

    一.移动监控的原理 通过获取摄像头图像,比较前后每一帧的图像数据,从而实现移动物体监控.所有移动监控原理都是这样,只是图像帧的对比的算法不一样. 二.移动物体监控系统的实现 选择开源的移动监控软件mo ...

  5. WebLogic 省略项目名称

    希望 WebLogic 部署的项目,不需要输入项目名,直接通过IP端口访问. 在 WEB-INF 目录下添加文件 weblogic.xml <?xml version="1.0&quo ...

  6. Linux文本处理详细教程

    1. 文本处理 本节将介绍Linux下使用Shell处理文本时最常用的工具: find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk: 提供的例子和参数都是 ...

  7. FreeAnchor 国科大

  8. 2020-07-14:es用过冷热分离吗?假如现在有些数据热变冷,有些数据冷变热,怎么解决?

    福哥答案2020-07-14: 热变冷: 有x台机器tag设置为hot. 有y台机器tag设置为cool. hot集群中只存最近两天的. 有一个定时任务每天将前一天的索引标记为cool. es看到有新 ...

  9. Java 创建、刷新Excel透视表/设置透视表行折叠、展开

    透视表是依据已有数据源来创建的交互式表格,我们可在excel中创建透视表,也可编辑已有透视表.本文以创建透视表.刷新透视表以及设置透视表的行展开或折叠为例,介绍具体的操作方法. 所需工具:Free S ...

  10. ElementUi——el-select下拉框

    <el-select v-model="ruleForm.status" placeholder="请选择状态" @change="styleC ...