1. 程式人生 > >android data binding jetpack V

android data binding jetpack V

rec view anim 就是 arraylist hold binding 要求 color

來實現一個recyclerview綁定。

看了例子理一下思路。

技術分享圖片

第一個關系:

item 與itemview 數據與展示綁定。

recyclerview 更新數據和UI過程是:獲取holder類型->產生holder->獲取holder()->holder+data->展示。

代碼在adapter中執行。

1.獲取某個位置的holder類型。

 @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

2.創建holder

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {   return viewHolder;
    }

3.綁定數據並展示出來。

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       
    }

現在使mvvm 那麽

第一步:在createholder的時候要確定 binding 與xml的綁定關系。

第二步:在onBindViewHolder中讓關系實現並展示。

看示例代碼怎麽實現第一步

  @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     //讀取xml對內存 LayoutInflater inflater
= LayoutInflater.from(parent.getContext()); //建立綁定關系
     ViewDataBinding binding
= DataBindingUtil.inflate(inflater, layoutId, parent, false); //建立一下holder
     ViewHolder viewHolder
= new ViewHolder(binding.getRoot()); //把綁定關系記錄在holder的變量裏。
     viewHolder.setBinding(binding);
return viewHolder; }
 @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
     //執行綁定,給綁定關系設置數據。 holder.getBinding().setVariable(brId,mDatas.get(position));
     //讓綁定生效。 holder.getBinding().executePendingBindings(); }
<?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>

        <import type="com.ht.jetpack.model.Student" />

        <variable
            name="student"
            type="Student" />
    </data>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:layout_width="280dp"
            android:layout_height="210dp"
            android:layout_margin="10dp"
            android:scaleType="centerCrop"
            app:studentAvatar="@{student.resId}" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:gravity="center"
            android:text="@{student.name}"
            android:textSize="18sp" />

    </LinearLayout>
</layout>

xml是綁定關系。

整個過程:加xml->產生綁定關->滾動到某個位置->獲取綁定關系,給綁定加入數據->展示

聲明的變量:

技術分享圖片

給變量賦值:

技術分享圖片

brId是技術分享圖片

就是xml裏聲明的模型變量。

難點:

技術分享圖片

這個之前沒有見過。自定義了一個屬性,進行了綁定。

具定實現在這兒:

技術分享圖片

這個需要求單獨一下塊內容去學習。

看一下運行效果:

技術分享圖片

以下是實例代碼:要運行請自己找幾張圖加到drawable裏命名一致就可以了。

package com.ht.jetpack.adapter;


import android.databinding.BindingAdapter;
import android.widget.ImageView;

public class BindingUtil {

    @BindingAdapter("bind:studentAvatar")
    public static void showImageByUrl(final ImageView imageView, int resId) {
        imageView.setImageResource(resId);
    }
}
package com.ht.jetpack.adapter;


import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

public class InitRecyclerView {

    public static void initLinearLayoutVERTICAL(Context context, RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    public static void initLinearLayoutWithoutDivid(Context context, RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    public static void initLinearLayoutHorizontal(Context context, RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    public static void initStaggered(Context context, RecyclerView recyclerView) {
        StaggeredGridLayoutManager sgm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(sgm);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}
package com.ht.jetpack.adapter;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;


import java.util.List;

/**
 * Created by hongtao
 */
public class MySimpleAdapter<T> extends RecyclerView.Adapter<ViewHolder>{

    private List<T> mDatas;

    private int layoutId;

    private int brId;

    public MySimpleAdapter(List<T> mDatas, int layoutId, int brId) {
        this.mDatas = mDatas;
        this.layoutId = layoutId;
        this.brId = brId;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(binding.getRoot());
        viewHolder.setBinding(binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.getBinding().setVariable(brId,mDatas.get(position));
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }
}
package com.ht.jetpack.adapter;

import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by hongtao .
 */
public class ViewHolder extends RecyclerView.ViewHolder {

    private ViewDataBinding binding;

    public ViewDataBinding getBinding() {
        return binding;
    }

    public void setBinding(ViewDataBinding binding) {
        this.binding = binding;
    }

    public ViewHolder(View itemView) {
        super(itemView);
    }
}
   RecyclerView recyclerView = (RecyclerView) findViewById(R.id.show_list);

        InitRecyclerView.initLinearLayoutWithoutDivid(this, recyclerView);
        List<Student> students = new ArrayList<>();
        Student student = new Student(R.drawable.tx2, "Kate");
        students.add(student);
        student = new Student(R.drawable.tx3, "tom");
        students.add(student);
        student = new Student(R.drawable.tx4, "Johnson");
        students.add(student);
        student = new Student(R.drawable.tx5, "Make");
        students.add(student);

        MySimpleAdapter<Student> adapter = new MySimpleAdapter<>(students, R.layout.student_item, BR.student);
        recyclerView.setAdapter(adapter);
在main layout裏聲明   
<android.support.v7.widget.RecyclerView android:id="@+id/show_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/navigation" android:layout_below="@id/tv" android:scrollbars="vertical" />

android data binding jetpack V