Android dataBinding與ListView及事件詳解
今天來了解一下Android最新給我們帶來的數(shù)據(jù)綁定框架——Data Binding Library。數(shù)據(jù)綁定框架給我們帶來了更大的方便性,以前我們可能需要在Activity里寫很多的findViewById,煩人的代碼也增加了我們代碼的耦合性,現(xiàn)在我們馬上就可以拋棄那么多的findViewById。說到這里,有人可能會有個疑問:我使用一些注解框架也可以不用findViewById啊,是的,但是注解注定要拖慢我們代碼的速度,Data Binding則不會,官網(wǎng)文檔說還會提高解析XML的速度,最主要的Data Binding并不是單單減少了我們的findViewById,更多好處請往下看文章。
2015年Google IO大會分布了DataBinding庫,能夠更快捷便利的實現(xiàn)MVVM結(jié)構(gòu)模式。但是,通過對DataBinding的學習,其中踩過得坑,今天要在這里記錄一下。對于DataBinding一些比較基礎(chǔ)的使用,在這里就不在記錄了,畢竟現(xiàn)在Google一下,出來很多的教程,而且,android developer官網(wǎng)中,也已經(jīng)對其基本使用方法做了詳細介紹,有英語基礎(chǔ)的童鞋,還是去看比較官方的文章。
關(guān)于配置環(huán)境:
2.0以上的 Android Studio 已經(jīng)內(nèi)置了對 Android Data Binding 框架的支持,配置起來也很簡單,只需要在 app 的 build.gradle 文件中添加下面的內(nèi)容就好了
1 dataBinding{
2 enabled = true
3 }
但是,gradle的版本,至少得是1.5.0以上,否則配置會很麻煩。因為本人使用的Android studio版本是2.1.3,gradle也更改成了2.1.3,所以,不需要做過多的設(shè)置。但是有一點,Android studio對DataBinding的支持還不是完全的兼容,有些地方確實有點坑。
關(guān)于使用:
最近,把之前寫的一個小項目,更改成了DataBinding的架構(gòu)模式。感覺Android studio2.1.3版本已經(jīng)很新了,但是對于一些屬性的提示還不是很好,并不是完全支持的。比較基礎(chǔ)的使用方法,在這里就不在提了,主要是寫一下對ListView以及GridView的使用,還有就是對adapter的寫法,以及點擊跳轉(zhuǎn)的事件。
首先,先是寫一個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進行數(shù)據(jù)的綁定。
然后,最主要的,其實就是適配器的寫法。在以往的寫法中,BaseAdapter肯定需要ViewHolder來進行視圖的綁定,并且做緩存。那么,在DataBinding中,完全不需要ViewHolder,而且,針對單布局的話,完全可以寫個通用的adapter,針對一般的小項目,這個adapter完全的夠用,那么,現(xiàn)在先來隨便寫一個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}",就能進行變量的賦值,這個在基礎(chǔ)用法中都有說明,這里就不在論述。下面就是重點了,關(guān)于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設(shè)置好以后,只需要在Activity中加入這樣兩句話就可以:
1 ListAdapter<UserBean> adapter = new ListAdapter<>(MainActivity.this, list, R.layout.item, BR.userbean);
2 binding.setAdapter(adapter);
binding怎么來的,這里就不在論述,請大家去看基礎(chǔ)使用方法。那么,在寫一個通用的adapter的時候,我們可以看到ListAdapter的泛型所代表的,其實就是一個Bean文件,是你需要賦值的那個文件。list代表的是一個List的列表值,這個列表可以是你在Json解析出來得列表值,也可以是你通過list.add所附的值,這些就要看你項目的需要了。最坑的地方在BR上,BR說起來就跟項目本身會產(chǎn)生的R文件是一個道理,只不過,BR是DataBinding所產(chǎn)生的一個R文件,也需要導(dǎo)入一個BR的包,當然,如果項目沒什么問題的,Android studio會提醒這個BR值導(dǎo)包的。我踩到的坑是,明明代碼中沒有任何問題,也沒有錯出現(xiàn),第一次運行成功了,第二次在運行的時候,就提示BR文件找不到,包刪了重新導(dǎo)都導(dǎo)不進去,clean一下不管用,包還是導(dǎo)不進去,Rebuild一下,提示找不到BR包,怎么都過不去。最后我只能把整個Android studio關(guān)掉在重新打開,發(fā)現(xiàn)BR包導(dǎo)進去了,然后也沒BUG了,運行也成功了。。。所以,如果你也遇到這種情況了,就請關(guān)閉Android studio并且重新打開一下,如果還沒好,就證明你的程序其實是有錯誤的,仔細找找就好。差不多就這個樣子吧。
下面還有一個問題,那就是關(guān)于點擊跳轉(zhuǎn)的問題。在其他的一些教程里面,可能只寫到了onClick事件的綁定,其中能實現(xiàn)的,就是改變當前數(shù)值或者字段。但是,還沒一些教程來講如何進行跳轉(zhuǎn)。現(xiàn)在我就來講一下跳轉(zhuǎn)如何實現(xiàn),先看代碼:
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方法。之前在調(diào)用setOnItemClickListener方法的時候,先是定義一個ListView的變量名,然后findByViewId來關(guān)聯(lián)上xml文件的ListView的ID值,然后才能調(diào)用其方法。用了DataBinding以后,只要用binding.listView就可以直接調(diào)用點擊事件,完全不需要在findByViewId,而listView其實就是xml里面的ID值,而這個變形的ID值其實是DataBinding根據(jù)ID值自動生成的,你只需要記得你起的名字是什么,根據(jù)大概的規(guī)律來找到自己定義的ID就好,這里并沒有什么難度。不光ListView可以這樣用,這個同樣適用于GridView。我在項目中也用到了GridView,親測這個ListAdapter同樣適用于GridView。并且,在我的項目里,是一個頁面用到了兩個GridView,只需要在data中定義兩個不同的variable值,并且name的定義名稱要定義不同的名字,這樣就可以同時使用一個ListAdapter了,后期我會將源碼放上來,這里只是記錄一下我使用的方法,以及需要注意的地方。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android 給應(yīng)用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)
本文主要介紹了Android 給應(yīng)用程序的icon添加未讀消息個數(shù)提示(紅圈內(nèi)數(shù)字)的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
實現(xiàn)Android 獲取cache緩存的目錄路徑的方法
這篇文章主要介紹了實現(xiàn)Android 獲取cache緩存的目錄路徑的方法的相關(guān)資料,這里實現(xiàn)一個靜態(tài)類來實現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
Android入門之實現(xiàn)手工發(fā)送一個BroadCast
這篇文章主要通過手工來發(fā)送一條BroadCast進一步來帶大家深入了解BroadCast,文中的示例代碼講解詳細,對我們學習Android有一定幫助,感興趣的可以收藏一下2022-12-12
android 使用瀏覽器打開指定頁面的實現(xiàn)方法
這篇文章主要介紹了android 使用瀏覽器打開指定頁面的實現(xiàn)方法,本文通過實例文字說明的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié)
這篇文章主要介紹了Android應(yīng)用開發(fā)中數(shù)據(jù)的保存方式總結(jié),包括對ROM、SD卡、SharedPreference這三種方式實現(xiàn)的核心代碼的精選,需要的朋友可以參考下2016-02-02
Android Service服務(wù)不被停止詳解及實現(xiàn)
這篇文章主要介紹了Android Service服務(wù)不被停止詳解及實現(xiàn)的相關(guān)資料,有很多應(yīng)用在設(shè)置運行中會被直接停止掉,這里就提供一個方法一直運行,需要的朋友可以參考下2016-11-11
Android使用個推實現(xiàn)三方應(yīng)用的推送功能
這篇文章主要為大家詳細介紹了Android使用個推實現(xiàn)三方應(yīng)用的推送功能,感興趣的小伙伴們可以參考一下2016-08-08

