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. 看了这篇文章,我搞懂了StringTable

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 String应该是Java ...

  2. 将map中的json转ObjecId

    我们直接从gridfs中取文件列表时取出来的文件_id为: "_id": { "timestamp": 1587091947, "counter&qu ...

  3. 移动物体监控系统-sprint4嵌入式web服务器开发

    一.BOA嵌入式服务器的移植 step1:下载BOA服务器并解压,进入boa下面的src目录,执行./configure生成必须的配置文件以及Makefile step2:修改Makefile文件 c ...

  4. 如何设置远程MongoDB!

    默认情况下V服务连接着本地mongoDB服务,如果想连接到其他mongoDB服务,请按如下设置: 方法一:通过控制台修改 进入控制台 http://x.x.x.x:xxxx/system/consol ...

  5. Linux常用命令之文件查找which、find、locate命令讲解

    在之前的课程中,我们介绍了Linux系统的常用文件处理命令和权限管理命令,今天我们继续来学习Linux操作系统的其他处理命令. 1.文件搜索命令 which 命令解释 命令名称:which 命令所在路 ...

  6. C#LeetCode刷题之#501-二叉搜索树中的众数​​​​​​​(Find Mode in Binary Search Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4086 访问. 给定一个有相同值的二叉搜索树(BST),找出 BS ...

  7. C#LeetCode刷题之#824-山羊拉丁文​​​​​​​(Goat Latin)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3971 访问. 给定一个由空格分割单词的句子 S.每个单词只包含大 ...

  8. Vue Elementui 表单必填项和非必填项label文字对齐的简单方式

    1. 不好的方式 很长时间以来都是用改写form-item样式来使得必填项和非必填项保证label对齐,这样需要改写系统样式,还要在相应的item上引用,代码量增多,示例如下(不推荐) <tem ...

  9. CSS 点击img 或者 div 增加抖动(shake)效果

    一般使用场景: 登录的错误验证 或者 强提醒 template 部分 <img id="barcode" :class="{ shaking: toShake}&q ...

  10. Java爬取先知论坛文章

    Java爬取先知论坛文章 0x00 前言 上篇文章写了部分爬虫代码,这里给出一个完整的爬取先知论坛文章代码. 0x01 代码实现 pom.xml加入依赖: <dependencies> & ...