zoukankan      html  css  js  c++  java
  • android 数据绑定(5) kotlin 的binding bug

    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:

      1 @get:Bindable
      2 var title: String = ""
      3     set(value) {
      4         field = value
      5         notifyPropertyChanged(BR.title)
      6     }

      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 解决方法

     1 apply plugin: 'com.android.application'
     2 
     3 apply plugin: 'kotlin-android'
     4 apply plugin: 'kotlin-kapt'
     5 apply plugin: 'kotlin-android-extensions'
     6 
     7 android {
     8 
     9     dataBinding {
    10         enabled = true
    11     }
    12 
    13 }
    14 
    15 dependencies {
    16 
    17     ...18 
    19     kapt 'com.android.databinding:compiler:3.2.0-alpha10'
    20 }
    • 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 ,不自动更新

    • 定义类
       1 public class Data extends BaseObservable {
       2 
       3     public int      icon    ;
       4     public String   key     ;
       5     public int      value   ;
       6     public String   test     = "";
       7 
       8     @Bindable public String getKey() { return key;}
       9     @Bindable public int getValue() { return value; }
      10     @Bindable public int getIcon() {
      11         return icon;
      12     }
      13     @Bindable public String getTest(){
      14         return test;
      15     }
      16 
      17     public void setKey(String key) {
      18         this.key = key;
      19         notifyPropertyChanged(BR.key);
      20     }
      21 
      22     public void setValue(int value) {
      23         this.value = value;
      24         notifyPropertyChanged(BR.value);
      25     }
      26     public void setIcon(int icon) {
      27         this.icon = icon;
      28         notifyPropertyChanged(BR.icon);
      29     }
      30     public void setTest(String ss){
      31         this.test = ss;
      32         notifyPropertyChanged(BR.test);
      33     }
      34 
      35     @Override
      36     public String toString() {
      37         return "key = " + key + " value = " + value;
      38     }
      39 }
    • 布局中
       1         <TextView
       2             android:id="@+id/data_key"
       3             android:layout_width="wrap_content"
       4             android:layout_height="wrap_content"
       5             android:layout_marginStart="32dp"
       6             android:layout_marginLeft="32dp"
       7             android:layout_marginTop="32dp"
       8             android:text='@{data.key}'
       9             app:layout_constraintStart_toStartOf="@+id/data_title"
      10             app:layout_constraintTop_toBottomOf="@+id/data_title" />
      11 
      12         <TextView
      13             android:id="@+id/data_value"
      14             android:layout_width="wrap_content"
      15             android:layout_height="wrap_content"
      16             android:layout_marginStart="16dp"
      17             android:layout_marginLeft="16dp"
      18             android:text='@{String.valueOf(data.value)}'
      19             app:layout_constraintStart_toEndOf="@+id/data_key"
      20             app:layout_constraintTop_toTopOf="@+id/data_key" />
    • 修改data值
      1     fun onDataThreadMainClicked(view: View){
      2         val random = (Math.random() * 1000).toInt()
      3         data.key = "新Main key$random"
      4         data.value = random
      5         binding.data = data
      6     }

      结果:界面不刷新 

    3.2 解决办法

    1      fun onDataThreadMainClicked(view: View){
    2         val random = (Math.random() * 1000).toInt()
    3         data.key = "新Main key$random"
    4         data.value = random
    5         binding.data = data
    6     }

    或者

    1     fun onDataThreadMainClicked(view: View){
    2         val random = (Math.random() * 1000).toInt()
    3         data.key = "新Main key$random"
    4         data.value = random
    5         binding.invalidateAll()
    6     }

    3.3 伴生对象的成员也不自动更新

    也要手动   binding.invalidateAll()

  • 相关阅读:
    IIS无法显示 XML 页
    asp.net实现sql存取图片
    微软的面试题
    IIS配置.Net问题大全
    Asp.Net调用WebService
    生活本无常,前路更精彩
    【转载】碰到讨厌的老板怎么办
    xxxx不必xx尽,留些xxxx
    【BLOG】Mr梵谷
    机会,从来都是留给有准备的你
  • 原文地址:https://www.cnblogs.com/mhbs/p/11874942.html
Copyright © 2011-2022 走看看