android data binding jetpack V 实现recyclerview 绑定
android data binding jetpack VIII BindingConversion
android data binding jetpack VII @BindingAdapter
android data binding jetpack V 实现recyclerview 绑定
android data binding jetpack IV 绑定一个方法另一种写法和参数传递
android data binding jetpack III 绑定一个方法
android data binding jetpack II 动态数据更新
android data binding jetpack I 环境配置 model-view 简单绑定
来实现一个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 实现recyclerview 绑定的更多相关文章
- android data binding jetpack IV 绑定一个方法另一种写法和参数传递
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack I 环境配置 model-view 简单绑定
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack VIII BindingConversion
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack VII @BindingAdapter
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack VI 清理一些概念。BR 运算表达式
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack II 动态数据更新
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- android data binding jetpack III 绑定一个方法
第三篇 给view绑定一下方法响应. (补充:1.被绑定的方法必须是public的. 1.绑定方法可以用主语法.也可以用以下双冒号方式“::” android:onClick="@{pr ...
- android data binding jetpack VIIII 第一坑
<LinearLayout android:id="@+id/ll_item_home_page_pics" android:layout_width="wrap_ ...
- Android开发教程 - 使用Data Binding(六)RecyclerView Adapter中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
随机推荐
- js 操作对象的小技巧
来源:https://www.w3cplus.com/javascript/javascript-tips.html 1.使用...运算符合并对象或数组中的对象 同样使用ES的...运算符可以替代人工 ...
- mybatis批量更新表setting parameters 错误
mybatis中想用 foreach标签 批量update set表 下面是mapper.xml <update id="updateMonitorById" paramet ...
- Linux 下vim命令详解
原博文:https://www.cnblogs.com/zknublx/p/6058679.html 高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx ...
- 第二章· MySQL体系结构管理
一.客户端与服务器模型  1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户端程序(/application/mysql/bin) mysql mysqladmin mysqld ...
- maven报错 java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector, see the following errors
2 errors java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector, ...
- linux获取某一个网卡的ipv4地址
ip a show ens33 | grep inet | grep -v inet6 | awk '{print $2}' | awk -F '/' '{print $1}'
- Linux 之Ubuntu在VM中安装(桌面版)
1.安装系统 https://jingyan.baidu.com/article/14bd256e0ca52ebb6d26129c.html 2.安装VM Tools https://jingyan. ...
- The Tower HDU - 6559 (解析几何)
The Tower HDU - 6559 The Tower shows a tall tower perched on the top of a rocky mountain. Lightning ...
- dedecms织梦后台发布文章提示“标题不能为空”的解决办法
V5.7登录后台后,发布英文标题没问题,发布中文会提示“标题不能为空”. 原因:htmlspecialchars在php5.4默认为utf8编码,gbk编码字符串经 htmlspecialchars ...
- 【2019中国大学生程序设计竞赛-女生专场】C - Function
原题 韦神提供的思路orz 首先一个显然的性质,所有的c可以提出来,方程变成ax^2+bx的形式 因为x的值是离散的,而m的值又不大 所以一开始让x都为1(注意!x是正整数),然后每次挑一个x让他加一 ...