欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android dataBinding與ListView及事件詳解

 更新時(shí)間:2016年10月18日 16:00:45   投稿:lqh  
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下

今天來了解一下Android最新給我們帶來的數(shù)據(jù)綁定框架——Data Binding Library。數(shù)據(jù)綁定框架給我們帶來了更大的方便性,以前我們可能需要在Activity里寫很多的findViewById,煩人的代碼也增加了我們代碼的耦合性,現(xiàn)在我們馬上就可以拋棄那么多的findViewById。說到這里,有人可能會(huì)有個(gè)疑問:我使用一些注解框架也可以不用findViewById啊,是的,但是注解注定要拖慢我們代碼的速度,Data Binding則不會(huì),官網(wǎng)文檔說還會(huì)提高解析XML的速度,最主要的Data Binding并不是單單減少了我們的findViewById,更多好處請(qǐng)往下看文章。

2015年Google IO大會(huì)分布了DataBinding庫,能夠更快捷便利的實(shí)現(xiàn)MVVM結(jié)構(gòu)模式。但是,通過對(duì)DataBinding的學(xué)習(xí),其中踩過得坑,今天要在這里記錄一下。對(duì)于DataBinding一些比較基礎(chǔ)的使用,在這里就不在記錄了,畢竟現(xiàn)在Google一下,出來很多的教程,而且,android developer官網(wǎng)中,也已經(jīng)對(duì)其基本使用方法做了詳細(xì)介紹,有英語基礎(chǔ)的童鞋,還是去看比較官方的文章。

關(guān)于配置環(huán)境:

2.0以上的 Android Studio 已經(jīng)內(nèi)置了對(duì) Android Data Binding 框架的支持,配置起來也很簡(jiǎn)單,只需要在 app 的 build.gradle 文件中添加下面的內(nèi)容就好了

1 dataBinding{
2   enabled = true
3 }

但是,gradle的版本,至少得是1.5.0以上,否則配置會(huì)很麻煩。因?yàn)楸救耸褂玫腁ndroid studio版本是2.1.3,gradle也更改成了2.1.3,所以,不需要做過多的設(shè)置。但是有一點(diǎn),Android studio對(duì)DataBinding的支持還不是完全的兼容,有些地方確實(shí)有點(diǎn)坑。

關(guān)于使用:

最近,把之前寫的一個(gè)小項(xiàng)目,更改成了DataBinding的架構(gòu)模式。感覺Android studio2.1.3版本已經(jīng)很新了,但是對(duì)于一些屬性的提示還不是很好,并不是完全支持的。比較基礎(chǔ)的使用方法,在這里就不在提了,主要是寫一下對(duì)ListView以及GridView的使用,還有就是對(duì)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,來對(duì)ListView或者GridView進(jìn)行數(shù)據(jù)的綁定。

然后,最主要的,其實(shí)就是適配器的寫法。在以往的寫法中,BaseAdapter肯定需要ViewHolder來進(jìn)行視圖的綁定,并且做緩存。那么,在DataBinding中,完全不需要ViewHolder,而且,針對(duì)單布局的話,完全可以寫個(gè)通用的adapter,針對(duì)一般的小項(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中加入這樣兩句話就可以:

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

binding怎么來的,這里就不在論述,請(qǐng)大家去看基礎(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)行也成功了。。。所以,如果你也遇到這種情況了,就請(qǐng)關(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,親測(cè)這個(gè)ListAdapter同樣適用于GridView。并且,在我的項(xiàng)目里,是一個(gè)頁面用到了兩個(gè)GridView,只需要在data中定義兩個(gè)不同的variable值,并且name的定義名稱要定義不同的名字,這樣就可以同時(shí)使用一個(gè)ListAdapter了,后期我會(huì)將源碼放上來,這里只是記錄一下我使用的方法,以及需要注意的地方。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論