recyclerView 中的数据绑定
databinding in recyclerView
我想在我的简单项目中使用 DataBinding
。进入我的 Recyclerview
适配器布局(rv_color_item.xml)项根据 android wiki 我写了这段代码:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="shahramColor"
type="com.groot.rang.model.ShahramColor"/>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.groot.rang.PagerColorActivity">
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="18dp"
app:cardElevation="8dp">
<ImageView
android:id="@+id/imvItemColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@{shahramColor.color}" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</layout>
现在在我的适配器中的 onBindViewHolder 方法中,我编写了这些代码:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ShahramColor color = colors.get(position);
if (holder instanceof ColorViewHolder) {
((ColorViewHolder) holder).binding.setShahramColor(color);
但是当我想在模拟器中 运行 项目时,我得到了这个错误:
Error:(10, 34) error: cannot find symbol class RvColorItemBinding
当我删除
<data>
<variable
name="shahramColor"
type="com.groot.rang.model.ShahramColor"/>
</data>
和
android:background="@{shahramColor.color}"
我可以在模拟器中 运行 应用程序,没有出现错误,一切正常 correctly.why 我有错误?
这是我的 pojo class:
public class ShahramColor extends RealmObject implements Serializable{
private String refrenceColor;
private String color;
... setter and getter
*********************编辑 ******************
我已将此添加到 gradle:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
dataBinding {
enabled true
}
compileSdkVersion 26
defaultConfig {...
适配器class:
public class ColorsRecyclerViewAdapter extends RecyclerView.Adapter<ColorsRecyclerViewAdapter.MyViewHolder> {
private List<ShahramColor> data;
public class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private final ViewDataBinding binding;
public MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj) {
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public ColorsRecyclerViewAdapter(List<ShahramColor> myDataset) {
data = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.rv_color_item, parent, false);
// set the view's size, margins, paddings and layout parameters
return new MyViewHolder(binding);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final ShahramColor colors = data.get(position);
holder.bind(colors);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return data.size();
}
}
型号class:
import android.databinding.BaseObservable;
import android.databinding.Bindable;
public class TemperatureData extends BaseObservable {
private String location;
private String celsius;
private String url;
public TemperatureData(String location, String celsius, String url) {
this.location = location;
this.celsius = celsius;
this.url = url;
}
@Bindable
public String getCelsius() {
return celsius;
}
@Bindable
public String getLocation() {
return location;
}
@Bindable
public String getUrl() {
return url;
}
public void setLocation(String location) {
this.location = location;
notifyPropertyChanged(BR.location);
}
public void setCelsius(String celsius) {
this.celsius = celsius;
notifyPropertyChanged(BR.celsius);
}
public void setUrl(String url) {
this.url = url;
notifyPropertyChanged(BR.url);
}
}
我想在我的简单项目中使用 DataBinding
。进入我的 Recyclerview
适配器布局(rv_color_item.xml)项根据 android wiki 我写了这段代码:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="shahramColor"
type="com.groot.rang.model.ShahramColor"/>
</data>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.groot.rang.PagerColorActivity">
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="18dp"
app:cardElevation="8dp">
<ImageView
android:id="@+id/imvItemColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@{shahramColor.color}" />
</android.support.v7.widget.CardView>
</RelativeLayout>
</layout>
现在在我的适配器中的 onBindViewHolder 方法中,我编写了这些代码:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ShahramColor color = colors.get(position);
if (holder instanceof ColorViewHolder) {
((ColorViewHolder) holder).binding.setShahramColor(color);
但是当我想在模拟器中 运行 项目时,我得到了这个错误:
Error:(10, 34) error: cannot find symbol class RvColorItemBinding
当我删除
<data>
<variable
name="shahramColor"
type="com.groot.rang.model.ShahramColor"/>
</data>
和
android:background="@{shahramColor.color}"
我可以在模拟器中 运行 应用程序,没有出现错误,一切正常 correctly.why 我有错误?
这是我的 pojo class:
public class ShahramColor extends RealmObject implements Serializable{
private String refrenceColor;
private String color;
... setter and getter
*********************编辑 ******************
我已将此添加到 gradle:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
dataBinding {
enabled true
}
compileSdkVersion 26
defaultConfig {...
适配器class:
public class ColorsRecyclerViewAdapter extends RecyclerView.Adapter<ColorsRecyclerViewAdapter.MyViewHolder> {
private List<ShahramColor> data;
public class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private final ViewDataBinding binding;
public MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj) {
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public ColorsRecyclerViewAdapter(List<ShahramColor> myDataset) {
data = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.rv_color_item, parent, false);
// set the view's size, margins, paddings and layout parameters
return new MyViewHolder(binding);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final ShahramColor colors = data.get(position);
holder.bind(colors);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return data.size();
}
}
型号class:
import android.databinding.BaseObservable;
import android.databinding.Bindable;
public class TemperatureData extends BaseObservable {
private String location;
private String celsius;
private String url;
public TemperatureData(String location, String celsius, String url) {
this.location = location;
this.celsius = celsius;
this.url = url;
}
@Bindable
public String getCelsius() {
return celsius;
}
@Bindable
public String getLocation() {
return location;
}
@Bindable
public String getUrl() {
return url;
}
public void setLocation(String location) {
this.location = location;
notifyPropertyChanged(BR.location);
}
public void setCelsius(String celsius) {
this.celsius = celsius;
notifyPropertyChanged(BR.celsius);
}
public void setUrl(String url) {
this.url = url;
notifyPropertyChanged(BR.url);
}
}