gpt4 book ai didi

android - recyclerView 中的数据绑定(bind)

转载 作者:搜寻专家 更新时间:2023-11-01 09:28:44 24 4
gpt4 key购买 nike

我想在我的简单项目中使用DataBinding。进入我的 Recyclerview Adapter layout(rv_color_item.xml) item 根据 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}"

我可以在模拟器中运行应用程序并且没有出现错误并且一切正常。为什么我会出错?

这是我的 pojo 类:

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 {...

最佳答案

适配器类:

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();
}

}

模型类:

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);
}
}

关于android - recyclerView 中的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48941174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com