2016-10-16 3 views
1

두 개의 서로 다른 항목 레이아웃에 동일한 어댑터를 사용합니다. 하나는 그리드 용이고, 하나는 선형 디스플레이 용입니다. itemLayout을 사용하여 레이아웃 ID를 어댑터에 전달하고 있습니다. 그러나 데이터 바인딩을 올바르게 추가 할 수 없었습니다. 도와 줄 수있어?DataBinding : 동일한 모델 및 다른 레이아웃으로 RecyclerView 어댑터를 만드는 방법은 무엇입니까?

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

답변

0

여기

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

     } 
    } 
} 

이 항목의 레이아웃입니다 : 여기

지금까지 내 어댑터 노력 내 잘못이야. 이 부분은 완벽하게 작동합니다. 내 문제는 또한 데이터 바인딩을 사용하는 BaseActivity를 확장하는 것입니다. 충돌은 그와 관련이 있습니다. 내 활동에서 확장 된 BaseActivity를 제거한 후에도 효과가있었습니다. 그러나, 나는 그것에 또 다른 질문을 열었다. 당신이 그것을 볼 수 있다면, 나는 감사 할 것입니다. DataBinding: How to use BaseActivity