Search code examples
androidandroid-recyclerviewandroid-databinding

DataBinding: How to create RecyclerView Adapter with same model and different layout?


I use same adapter for two different item layouts. One is for grid, one is for linear display. I'm passing layout id with itemLayout to adapter. However, I wasn't able to add data binding properly. Could you help me out, please?

Here is my adapter effort so far:

public class OyuncuAdapter extends RecyclerView.Adapter<OyuncuAdapter.ViewHolder> {

    ArrayList<Oyuncu> oyuncuListesi;
    Context context;
    int itemLayout;

    public OyuncuAdapter(ArrayList<Oyuncu> oyuncuListesi, Context context, int itemLayout) {
        this.oyuncuListesi = oyuncuListesi;
        this.context=context;
        this.itemLayout = itemLayout;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {         
        View v = LayoutInflater.from(context).inflate(itemLayout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Oyuncu oyuncu = oyuncuListesi.get(position);
        holder.binding.setVariable(BR.oyuncu, oyuncu);
        holder.binding.executePendingBindings();

    }

    @Override
    public int getItemCount() {
        return oyuncuListesi.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ViewDataBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);

        }
    }
}

Here is the item layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="oyuncu"
            type="com.figengungor.suits.model.Oyuncu" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="@dimen/itemPadding"
        tools:background="@color/darkgrey">

        <ImageView
            android:id="@+id/foto"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:scaleType="centerCrop"
            app:imageResource="@{oyuncu.foto}"
            tools:src="@drawable/gabrielmacht" />

        <TextView
            android:id="@+id/isim"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:text="@{oyuncu.isim}"
            tools:text="Gabriel Macht" />

    </LinearLayout>

</layout>

The error I'm getting is not really helpful:

Caused by: java.lang.RuntimeException: view tag isn't correct on view:null

Solution

  • My bad. This part works perfectly. My problem is extending BaseActivity which also uses DataBinding. The crash is related to that. After removing extending BaseActivity from my activity, it worked. However, I opened another question realted to that. If you could look at that, I'll be grateful. DataBinding: How to use BaseActivity