2015年Google IO大会分布了DataBinding库,能够更快捷便利的实现MVVM结构模式。但是,通过对DataBinding的学习,其中踩过得坑,今天要在这里记录一下。对于DataBinding一些比较基础的使用,在这里就不在记录了,毕竟现在Google一下,出来很多的教程,而且,android developer官网中,也已经对其基本使用方法做了详细介绍,有英语基础的童鞋,还是去看比较官方的文章。如果英文基础不太好的,https://realm.io/cn/news/data-binding-android-boyar-mount/推荐这个博客,会有很大收获的,同时,谢谢棉花糖的这篇文章,解决了很多的疑惑。

关于配置环境:

2.0以上的 Android Studio 已经内置了对 Android Data Binding 框架的支持,配置起来也很简单,只需要在 app 的 build.gradle 文件中添加下面的内容就好了

 dataBinding{
enabled = true
}

但是,gradle的版本,至少得是1.5.0以上,否则配置会很麻烦。因为本人使用的Android studio版本是2.1.3,gradle也更改成了2.1.3,所以,不需要做过多的设置。但是有一点,Android studio对DataBinding的支持还不是完全的兼容,有些地方确实有点坑。

关于使用:

最近,把之前写的一个小项目,更改成了DataBinding的架构模式。感觉Android studio2.1.3版本已经很新了,但是对于一些属性的提示还不是很好,并不是完全支持的。比较基础的使用方法,在这里就不在提了,主要是写一下对ListView以及GridView的使用,还有就是对adapter的写法,以及点击跳转的事件。

首先,先是写一个ListView或者GridView的xml文件,代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable
name="adapter"
type="android.widget.BaseAdapter"/> </data> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:adapter="@{adapter}"/> </LinearLayout>
</layout>

重点在app:adapter="@{adapter}"这句话中,主要是自定义一个adapter,来对ListView或者GridView进行数据的绑定。

然后,最主要的,其实就是适配器的写法。在以往的写法中,BaseAdapter肯定需要ViewHolder来进行视图的绑定,并且做缓存。那么,在DataBinding中,完全不需要ViewHolder,而且,针对单布局的话,完全可以写个通用的adapter,针对一般的小项目,这个adapter完全的够用,那么,现在先来随便写一个adapter的item的xml文件,代码如下:

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <data>
<variable
name="userbean"
type="com.lqy.newtestdemo.UserBean"/>
</data> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"> <ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
app:imageUrl="@{userbean.picUrl}"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{userbean.title}"
android:textColor="@android:color/black"
android:textSize="20sp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@{userbean.ctime}"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@{userbean.description}"/>
</LinearLayout>
</RelativeLayout>
</layout>

可以看到,布局中,主要是通过data中的variable属性来标识一个变量名,在控件中,只需要android:text="@{userbean.title}",就能进行变量的赋值,这个在基础用法中都有说明,这里就不在论述。下面就是重点了,关于BaseAdapter的写法,废话不多说,直接上代码:

 package com.lqy.newtestdemo;

 import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; import java.util.List; /**
* 通用的adapter
* Created by LQY on 2016/10/10.
*/
public class ListAdapter<T> extends BaseAdapter {
private Context context;
private List<T> list;
private int layoutId;//单布局
private int variableId; public ListAdapter(Context context, List<T> list, int layoutId, int variableId) {
this.context = context;
this.list = list;
this.layoutId = layoutId;
this.variableId = variableId;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewDataBinding binding = null;
if (convertView == null){
binding =DataBindingUtil.inflate(LayoutInflater.from(context),layoutId,parent,false);
} else {
binding = DataBindingUtil.getBinding(convertView);
}
binding.setVariable(variableId,list.get(position));
return binding.getRoot();
}
}

在这里可以看到,完全看不到ViewHolder的踪迹,而且,只需几行的代码,就能将适配器写好,并且,可以用到多个ListView或者GridView中,adapter设置好以后,只需要在Activity中加入这样两句话就可以:

 ListAdapter<UserBean> adapter = new ListAdapter<>(MainActivity.this, list, R.layout.item, BR.userbean);
binding.setAdapter(adapter);

binding怎么来的,这里就不在论述,请大家去看基础使用方法。那么,在写一个通用的adapter的时候,我们可以看到ListAdapter的泛型所代表的,其实就是一个Bean文件,是你需要赋值的那个文件。list代表的是一个List的列表值,这个列表可以是你在Json解析出来得列表值,也可以是你通过list.add所附的值,这些就要看你项目的需要了。最坑的地方在BR上,BR说起来就跟项目本身会产生的R文件是一个道理,只不过,BR是DataBinding所产生的一个R文件,也需要导入一个BR的包,当然,如果项目没什么问题的,Android studio会提醒这个BR值导包的。我踩到的坑是,明明代码中没有任何问题,也没有错出现,第一次运行成功了,第二次在运行的时候,就提示BR文件找不到,包删了重新导都导不进去,clean一下不管用,包还是导不进去,Rebuild一下,提示找不到BR包,怎么都过不去。最后我只能把整个Android studio关掉在重新打开,发现BR包导进去了,然后也没BUG了,运行也成功了。。。所以,如果你也遇到这种情况了,就请关闭Android studio并且重新打开一下,如果还没好,就证明你的程序其实是有错误的,仔细找找就好。差不多就这个样子吧。

下面还有一个问题,那就是关于点击跳转的问题。在其他的一些教程里面,可能只写到了onClick事件的绑定,其中能实现的,就是改变当前数值或者字段。但是,还没一些教程来讲如何进行跳转。现在我就来讲一下跳转如何实现,先看代码:

binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, WebActivity.class);
intent.putExtra("url", list.get(position).getUrl());
startActivity(intent);
}
});

在Activity页面,还可以使用setOnItemClickListener方法。之前在调用setOnItemClickListener方法的时候,先是定义一个ListView的变量名,然后findByViewId来关联上xml文件的ListView的ID值,然后才能调用其方法。用了DataBinding以后,只要用binding.listView就可以直接调用点击事件,完全不需要在findByViewId,而listView其实就是xml里面的ID值,而这个变形的ID值其实是DataBinding根据ID值自动生成的,你只需要记得你起的名字是什么,根据大概的规律来找到自己定义的ID就好,这里并没有什么难度。不光ListView可以这样用,这个同样适用于GridView。我在项目中也用到了GridView,亲测这个ListAdapter同样适用于GridView。并且,在我的项目里,是一个页面用到了两个GridView,只需要在data中定义两个不同的variable值,并且name的定义名称要定义不同的名字,这样就可以同时使用一个ListAdapter了,后期我会将源码放上来,这里只是记录一下我使用的方法,以及需要注意的地方。

有写的不对的地方希望大伙指出来,也希望我这篇文章能帮到正在DataBinding中挣扎的童鞋。

最新文章

  1. CSS基础篇之背景、过渡动画
  2. Oracle手边常用命令及操作语句
  3. Chrome54安装最新版Flash版本办法
  4. Swift语言的基础详解
  5. Wordpress如何开启用户注册功能
  6. angular 三目运算符 需要换色或style
  7. node.js http.get 和http.post 数据
  8. css+js自动化开发之第十五天
  9. .NET基础拾遗(5)反射1
  10. 操作Action
  11. 程序员眼里IE浏览器是什么样的
  12. One-Way Reform
  13. emacs命令记录
  14. hive中一些常用的sql语句
  15. ★Linux桌面系统技巧(作为客户端)
  16. 影响Arcmap运行效率的因素
  17. js 控制光标到指定位置
  18. tarjan算法总结
  19. timeline server介绍
  20. 常系数线性递推的第n项及前n项和 (Fibonacci数列,矩阵)

热门文章

  1. SDUT OJ 数据结构实验之链表三:链表的逆置
  2. 带权并查集 - How Many Answers Are Wrong
  3. JUnit4.13环境配置
  4. sed的基本用法
  5. JavaWeb学习笔记(五)—— request
  6. Spring学习笔记(二)——Spring相关配置&amp;属性注入&amp;Junit整合
  7. BZOJ - 2818 莫比乌斯反演 初步
  8. 【研究】Joomla二阶注入
  9. Python 时间格式转换
  10. css百分比单位