android data binding jetpack VIII BindingConversionhtml
android data binding jetpack VII @BindingAdapterjava
android data binding jetpack V 實現recyclerview 綁定android
android data binding jetpack IV 綁定一個方法另外一種寫法和參數傳遞app
android data binding jetpack III 綁定一個方法mvvm
android data binding jetpack II 動態數據更新ide
android data binding jetpack I 環境配置 model-view 簡單綁定post
來實現一個recyclerview綁定。學習
看了例子理一下思路。this
第一個關係:spa
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" />