我想使用數據綁定在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;
}
}
}