如何使用数据绑定将数据绑定到RecyclerView中的行?
我想使用数据绑定在RecyclerView行中显示一些图像和文本。代码运行时没有任何错误,但数据没有显示在RecyclerView中。如何使用数据绑定将数据绑定到RecyclerView中的行?
reward_item.xml
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View" />
<variable
name="recipes"
type="com.prasona.android.indiancusine.Model.NameModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/recipeimageView"
android:layout_toRightOf="@+id/recipeimageView"
android:text="@{recipes.recipeName}"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ImageView
android:id="@+id/recipeimageView"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/dinner"
app:imageUrl="@{image.imageUrl}"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</layout>
这里是我的适配器:
import android.databinding.BindingAdapter; import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* Created by admin on 14-12-2017.
*/
public class RewardAdapter extends RecyclerView.Adapter<RewardAdapter.ViewHolder> {
private List<SampleModel> model;
public RewardAdapter(List<SampleModel> model) {
this.model = model;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.rewarded_item, parent, false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ViewDataBinding viewDataBinding = holder.getViewDataBinding();
// viewDataBinding.setVariable(BR.image,model.get(position));
viewDataBinding.setVariable(BR.recipeName, model.get(position));
viewDataBinding.setVariable(BR.imageUrl, model.get(position));
}
@Override
public int getItemCount() {
return (null != model ? model.size() : 0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ViewDataBinding viewBinding;
public ViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
viewBinding = binding;
viewBinding.executePendingBindings();
}
public ViewDataBinding getViewDataBinding() {
return viewBinding;
}
}
}
回答:
目前还不清楚在所有什么是你想绑定。在你的代码中,你有SampleModel类,它似乎是你的行的模型,但是在你的布局中,你已经声明了一个Model.NameModel类型的变量配方。在行布局的后面,您还使用ImageView的图像变量。
我将假定您的行模型是您在适配器中使用的SampleModel类,它包含文本(用于TextView)和表示该条目的图像URL的字符串。为了使您应该结合工作:在代码
声明的SampleModel变量,并将其用于您的观点:
<data> <import type="android.view.View" />
<variable
name="recipes"
type="the.package.of.the.class.SampleModel" />
</data>
将在布局中使用:
<TextView ...
android:text="@{recipes.recipeName}"/>
<ImageView
...
app:imageUrl="@{recipes.imageUrl}"/>
方式的SampleModel类应该是扩展BaseObservable并展示我们上面使用的两个字段的类:
class SampleModel extends BaseObservable { private String recipeName;
private String imageUrl;
@Bindable
public String getRecipeName() {
return recipeName;
}
@Bindable
public String getImageUrl() {
return imageUrl;
}
}
适配器将需要改变使用精确的绑定类:
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RewardItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.rewarded_item, parent, false);
// update the ViewHolder constructor to receive a RewardItemBinding parameter instead of the super class
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RewardItemBinding viewDataBinding = holder.getViewDataBinding();
viewDataBinding.setRecipes(model.get(position));
}
这将设置数据从模型中的两种观点。问题出在ImageView上,它没有为我们在布局中使用的imageUrl属性设置setter,并且绑定框架不知道如何将数据绑定到视图。要处理这个问题,您可以使用如下的BindingAdapter:
// declare this method in one of your classes(notice the static modifier) @BindingAdapter("imageUrl")
public static void bindImage(ImageView imageView, String imageUrl) {
// use Glide to setup the image
}
以上是 如何使用数据绑定将数据绑定到RecyclerView中的行? 的全部内容, 来源链接: utcz.com/qa/261590.html