簡體   English   中英

DataBinding:如何創建具有相同模型和不同布局的RecyclerView Adapter?

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

我為兩個不同的項目布局使用相同的適配器。 一個用於網格,一個用於線性顯示。 我將帶有itemLayout的布局ID傳遞給適配器。 但是,我無法正確添加數據綁定。 你能幫幫我嗎?

這是我到目前為止的適配器工作:

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

        }
    }
}

這是項目布局:

<?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>

我得到的錯誤並沒有多大幫助:

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

我的錯。 這部分很完美。 我的問題是擴展BaseActivity,它也使用DataBinding。 崩潰與此有關。 從我的活動中刪除擴展BaseActivity后,它工作正常。 但是,我打開了另一個問題。 如果你能看一下,我將不勝感激。 DataBinding:如何使用BaseActivity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM