2

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


} 

回答

0

目前還不清楚在所有什麼是你想綁定。在你的代碼中,你有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 
}