Android 開發(fā)之dataBinding與ListView及事件
2015年Google IO大會(huì)分布了DataBinding庫,能夠更快捷便利的實(shí)現(xiàn)MVVM結(jié)構(gòu)模式。但是,通過對DataBinding的學(xué)習(xí),其中踩過得坑,今天要在這里記錄一下。對于DataBinding一些比較基礎(chǔ)的使用,在這里就不在記錄了,畢竟現(xiàn)在Google一下,出來很多的教程,而且,android developer官網(wǎng)中,也已經(jīng)對其基本使用方法做了詳細(xì)介紹,有英語基礎(chǔ)的童鞋,還是去看比較官方的文章。如果英文基礎(chǔ)不太好的,https://realm.io/cn/news/data-binding-android-boyar-mount/推薦這個(gè)博客,會(huì)有很大收獲的,同時(shí),謝謝棉花糖的這篇文章,解決了很多的疑惑。
關(guān)于配置環(huán)境:
2.0以上的 Android Studio 已經(jīng)內(nèi)置了對 Android Data Binding 框架的支持,配置起來也很簡單,只需要在 app 的 build.gradle 文件中添加下面的內(nèi)容就好了
dataBinding{ enabled = true }
但是,gradle的版本,至少得是1.5.0以上,否則配置會(huì)很麻煩。因?yàn)楸救耸褂玫腁ndroid studio版本是2.1.3,gradle也更改成了2.1.3,所以,不需要做過多的設(shè)置。但是有一點(diǎn),Android studio對DataBinding的支持還不是完全的兼容,有些地方確實(shí)有點(diǎn)坑。
關(guān)于使用:
最近,把之前寫的一個(gè)小項(xiàng)目,更改成了DataBinding的架構(gòu)模式。感覺Android studio2.1.3版本已經(jīng)很新了,但是對于一些屬性的提示還不是很好,并不是完全支持的。比較基礎(chǔ)的使用方法,在這里就不在提了,主要是寫一下對ListView以及GridView的使用,還有就是對adapter的寫法,以及點(diǎn)擊跳轉(zhuǎn)的事件。
首先,先是寫一個(gè)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>
重點(diǎn)在app:adapter="@{adapter}"這句話中,主要是自定義一個(gè)adapter,來對ListView或者GridView進(jìn)行數(shù)據(jù)的綁定。
然后,最主要的,其實(shí)就是適配器的寫法。在以往的寫法中,BaseAdapter肯定需要ViewHolder來進(jìn)行視圖的綁定,并且做緩存。那么,在DataBinding中,完全不需要ViewHolder,而且,針對單布局的話,完全可以寫個(gè)通用的adapter,針對一般的小項(xiàng)目,這個(gè)adapter完全的夠用,那么,現(xiàn)在先來隨便寫一個(gè)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屬性來標(biāo)識(shí)一個(gè)變量名,在控件中,只需要android:text="@{userbean.title}",就能進(jìn)行變量的賦值,這個(gè)在基礎(chǔ)用法中都有說明,這里就不在論述。下面就是重點(diǎn)了,關(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的蹤跡,而且,只需幾行的代碼,就能將適配器寫好,并且,可以用到多個(gè)ListView或者GridView中,adapter設(shè)置好以后,只需要在Activity中加入這樣兩句話就可以:
ListAdapter<UserBean> adapter = new ListAdapter<>(MainActivity.this, list, R.layout.item, BR.userbean); binding.setAdapter(adapter);
binding怎么來的,這里就不在論述,請大家去看基礎(chǔ)使用方法。那么,在寫一個(gè)通用的adapter的時(shí)候,我們可以看到ListAdapter的泛型所代表的,其實(shí)就是一個(gè)Bean文件,是你需要賦值的那個(gè)文件。list代表的是一個(gè)List的列表值,這個(gè)列表可以是你在Json解析出來得列表值,也可以是你通過list.add所附的值,這些就要看你項(xiàng)目的需要了。最坑的地方在BR上,BR說起來就跟項(xiàng)目本身會(huì)產(chǎn)生的R文件是一個(gè)道理,只不過,BR是DataBinding所產(chǎn)生的一個(gè)R文件,也需要導(dǎo)入一個(gè)BR的包,當(dāng)然,如果項(xiàng)目沒什么問題的,Android studio會(huì)提醒這個(gè)BR值導(dǎo)包的。我踩到的坑是,明明代碼中沒有任何問題,也沒有錯(cuò)出現(xiàn),第一次運(yùn)行成功了,第二次在運(yùn)行的時(shí)候,就提示BR文件找不到,包刪了重新導(dǎo)都導(dǎo)不進(jìn)去,clean一下不管用,包還是導(dǎo)不進(jìn)去,Rebuild一下,提示找不到BR包,怎么都過不去。最后我只能把整個(gè)Android studio關(guān)掉在重新打開,發(fā)現(xiàn)BR包導(dǎo)進(jìn)去了,然后也沒BUG了,運(yùn)行也成功了。。。所以,如果你也遇到這種情況了,就請關(guān)閉Android studio并且重新打開一下,如果還沒好,就證明你的程序其實(shí)是有錯(cuò)誤的,仔細(xì)找找就好。差不多就這個(gè)樣子吧。
下面還有一個(gè)問題,那就是關(guān)于點(diǎn)擊跳轉(zhuǎn)的問題。在其他的一些教程里面,可能只寫到了onClick事件的綁定,其中能實(shí)現(xiàn)的,就是改變當(dāng)前數(shù)值或者字段。但是,還沒一些教程來講如何進(jìn)行跳轉(zhuǎn)?,F(xiàn)在我就來講一下跳轉(zhuǎn)如何實(shí)現(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方法的時(shí)候,先是定義一個(gè)ListView的變量名,然后findByViewId來關(guān)聯(lián)上xml文件的ListView的ID值,然后才能調(diào)用其方法。用了DataBinding以后,只要用binding.listView就可以直接調(diào)用點(diǎn)擊事件,完全不需要在findByViewId,而listView其實(shí)就是xml里面的ID值,而這個(gè)變形的ID值其實(shí)是DataBinding根據(jù)ID值自動(dòng)生成的,你只需要記得你起的名字是什么,根據(jù)大概的規(guī)律來找到自己定義的ID就好,這里并沒有什么難度。不光ListView可以這樣用,這個(gè)同樣適用于GridView。我在項(xiàng)目中也用到了GridView,親測這個(gè)ListAdapter同樣適用于GridView。并且,在我的項(xiàng)目里,是一個(gè)頁面用到了兩個(gè)GridView,只需要在data中定義兩個(gè)不同的variable值,并且name的定義名稱要定義不同的名字,這樣就可以同時(shí)使用一個(gè)ListAdapter了,后期我會(huì)將源碼放上來,這里只是記錄一下我使用的方法,以及需要注意的地方。
以上所述是小編給大家介紹的Android 開發(fā)之dataBinding與ListView及事件,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android實(shí)現(xiàn)TCP客戶端接收數(shù)據(jù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)TCP客戶端接收數(shù)據(jù)的方法,較為詳細(xì)的分析了Android基于TCP實(shí)現(xiàn)客戶端接收數(shù)據(jù)的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-04-04Android入門之Glide顯示網(wǎng)絡(luò)圖片高版本的使用詳解
這篇文章主要為大家詳細(xì)介紹了Android中Glide顯示網(wǎng)絡(luò)圖片高版本的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02Flutter中g(shù)o_router路由管理的使用指南
go_router?是一個(gè)?Flutter?的第三方路由插件,相比?Flutter?自帶的路由,go_router?更加靈活,而且簡單易用,下面小編就來和大家聊聊go_router的使用吧2023-08-08Android實(shí)現(xiàn)去哪兒攜程地址互換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)去哪兒攜程地址互換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android實(shí)現(xiàn)網(wǎng)絡(luò)加載時(shí)的對話框功能
這篇文章主要介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)加載時(shí)的對話框功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
本篇文章主要介紹了Android Recyclerview 實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android利用Senser實(shí)現(xiàn)不同的傳感器
這篇文章主要為大家詳細(xì)介紹了Android利用Senser實(shí)現(xiàn)不同傳感器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02