zoukankan      html  css  js  c++  java
  • DataBinding注意事项Error parsing XML: duplicate attribute以及如何在listview中使用DataBinding

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5    >
     6 
     7     <TextView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:text="Hello World!"
    11  />
    12 
    13 </LinearLayout>

    1.反例:像这个布局文件,如果要使用DataBinding的话,最外层要改成layout布局的,

    但是我以为它最终要改成layout,所以我事先在加入complie和enabled代码前直接改成layout,

        //DataBinding
        compile "com.jakewharton.rxbinding:rxbinding:0.4.0"
        compile "com.jakewharton.rxbinding:rxbinding-design:0.4.0"
        compileSdkVersion 24
        buildToolsVersion "24.0.0"
        dataBinding {
            enabled = true
        }
    }

    导致在在加入complie和enabled代码后,点击右上角的Sync Now,一直报错

    C:androidASWorkSpaceHighlyTechnical	enicaluildintermediatesdata-binding-layout-outdebuglayoutactivity_main.xml
    Error:(11) Error parsing XML: duplicate attribute
    Error:Execution failed for task ':tenical:processDebugResources'.
    > com.android.ide.common.process.ProcessException: Failed to execute aapt
    Information:BUILD FAILED

    莫名奇妙的错误。

    搞了半天都不知道是什么原因,

    原来是我们要将根布局变成layout,还要去掉根布局的width和height

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <layout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     >
     5     <RelativeLayout
     6         android:layout_width="match_parent"
     7         android:layout_height="match_parent">
     8     <TextView
     9         android:id="@+id/tv_test"
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content"
    12         android:text="Hello World!" />
    13     </RelativeLayout>
    14 
    15 </layout>

     2.

    (1)获取到的DataBinding对象,是根据布局文件命名的

    ListItemHomeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(UIUtils.getContext()), R.layout.list_item_home, parent, false);

    而布局文件要先实现根布局为layout,

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <layout xmlns:android="http://schemas.android.com/apk/res/android"
     3     >
     4 <data>
     5     <variable
     6         name="ListBean"
     7         type="com.weizh_000.googleplay.domain.HomeBean.ListBean"/>
     8 
     9 </data>
    10 
    11 <TextView
    12     android:text="@{ListBean.downloadUrl}"
    13     android:layout_width="match_parent"
    14     android:layout_height="wrap_content"
    15     android:textColor="#000"
    16     android:id="@+id/textView" />
    17 </layout>

    然后build一遍,对象名就出来了,但是按ctrl+2+L出来的不是这个名,是

    ViewDataBinding,直接用这个也可以,或者自己手动敲对象名。

    (2)不仅对象名会创建,对象方法也会被创建。对象方法名是根据data标签中
    variable标签的name创建的,例如上面布局文件中的name为ListBean,那么就会创建一个方法名字为setListBean
     1         @Override
     2         public View getView(int position, View convertView, ViewGroup parent) {
     3             ListItemHomeBinding binding;
     4             if (convertView == null) {
     5                 binding = DataBindingUtil.inflate(LayoutInflater.from(UIUtils.getContext()), R.layout.list_item_home, parent, false);
     6                 convertView = binding.getRoot();
     7                 convertView.setTag(binding);
     8             } else {
     9                 binding = (ListItemHomeBinding) convertView.getTag();
    10             }
    11 
    12             binding.setListBean(getItem(position)); 
    13 return convertView; 14 }

    这是ListView的Adapter中的getView方法,同时也展示了如何在ListView中使用DataBinding。

    3.要有data的标签,而data的标签又要有一个类名,所以又得创建一个类,通常这个类是javabean类

  • 相关阅读:
    15. DML, DDL, LOGON 触发器
    5. 跟踪标记 (Trace Flag) 834, 845 对内存页行为的影响
    4. 跟踪标记 (Trace Flag) 610 对索引组织表(IOT)最小化日志
    14. 类似正则表达式的字符处理问题
    01. SELECT显示和PRINT打印超长的字符
    3. 跟踪标记 (Trace Flag) 1204, 1222 抓取死锁信息
    2. 跟踪标记 (Trace Flag) 3604, 3605 输出DBCC命令结果
    1. 跟踪标记 (Trace Flag) 1117, 1118 文件增长及空间分配方式
    0. 跟踪标记 (Trace Flag) 简介
    SpringBoot + Redis + Shiro 实现权限管理(转)
  • 原文地址:https://www.cnblogs.com/johnsonwei/p/5727771.html
Copyright © 2011-2022 走看看