2016-10-25 16 views
0

RecyclerViewで複数のビューを使用したいと考えています。 "pauze"という単語が渡されたときに "card_view_row_pauze"を使用したいと思います。 "pauze"という単語がないときに "card_view_row"を使いたいと思います。私はいくつかのことを試しました(コードに追加したコメントを参照してください)が、うまくいきませんでした。私はstackoverflowのいくつかの答えを見つけましたが、私は残念ながら、それを実装するように見えることはできません。どうすればいいですか?recyclerviewで複数のビューを表示する方法

MyRecyclerViewAdapter.java

package com.company.app; 

import android.app.Activity; 
import android.support.v7.widget.CardView; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class MyRecyclerViewAdapter extends RecyclerView .Adapter<MyRecyclerViewAdapter .DataObjectHolder> { 
    private ArrayList<DataObject> mDataset; 
    private static MyClickListener myClickListener; 
    private Activity mActivity; 

    public static class DataObjectHolder extends RecyclerView.ViewHolder implements View .OnClickListener { 
     TextView label; 
     TextView dateTime; 
     TextView tijd; 
     CardView cardView; 

     public DataObjectHolder(View itemView) { 
      super(itemView); 
      label = (TextView) itemView.findViewById(R.id.textView); 
      dateTime = (TextView) itemView.findViewById(R.id.textView2); 
      tijd = (TextView) itemView.findViewById(R.id.textView3); 
      cardView = (CardView) itemView.findViewById(R.id.card_view); 
      itemView.setOnClickListener(this); 
     } 

     @Override public void onClick(View v) { 
      myClickListener.onItemClick(getAdapterPosition(), v); 
     } 
    } 

    ViewGroup myParent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS 

    public void setOnItemClickListener(MyClickListener myClickListener) { 
     this.myClickListener = myClickListener; 
    } 

    public MyRecyclerViewAdapter(Activity activity, ArrayList<DataObject> myDataset) { 
     mDataset = myDataset; 
     mActivity = activity; 
    } 

    @Override public DataObjectHolder onCreateViewHolder(ViewGroup parent,int viewType) { 
     myParent = parent; // THIS I ADDED TO SUPPORT MULTIPLE VIEWS 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, parent, false); 
     DataObjectHolder dataObjectHolder = new DataObjectHolder(view); 
     return dataObjectHolder; 
    } 

    @Override 
    public void onBindViewHolder(DataObjectHolder holder, final int position) { 
     if (mDataset.get(position).getmText1().equals("Pauze")) { //HERE I WANT TO USE THE "card_view_row_pauze" VIEW WHEN THE PASSED TEXT CONTAINS "PAUZE" 
      View viewd = LayoutInflater.from(myParent.getContext()).inflate(R.layout.card_view_row_pauze, myParent, false); 
      DataObjectHolder dataObjectHolderd = new DataObjectHolder(viewd); 
      dataObjectHolderd.label.setText(mDataset.get(position).getmText1()); 
      dataObjectHolderd.tijd.setText(mDataset.get(position).getmText3()); 
     } 
     else { 
      holder.cardView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String myDescription = mDataset.get(position).getDescription(); 
        if (myDescription.length() >= 2) { 
         Toast.makeText(MainActivity.getMain().getApplicationContext(), myDescription, Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
      holder.label.setText(mDataset.get(position).getmText1()); 
      holder.dateTime.setText(mDataset.get(position).getmText2()); 
      holder.tijd.setText(mDataset.get(position).getmText3()); 
     } 
    } 

    public void addItem(final DataObject dataObj, final int index) {if (mActivity == null){return;} 

     mActivity.runOnUiThread(new Runnable() { 
      @Override public void run() { 
       mDataset.add(index, dataObj); 
       notifyItemInserted(index); 
      } 
     }); 
    } 

    public void deleteItem(int index) { 
     mDataset.remove(index); 
     notifyItemRemoved(index); 
    } 

    @Override public int getItemCount() {return mDataset.size();} 
    public interface MyClickListener {public void onItemClick(int position, View v);} 
} 

DataObject.java

package com.company.app; 

public class DataObject { 
    private String mText1; 
    private String mText2; 
    private String mText3; 
    private String mState; 
    private String mDescription; 

    DataObject (String text1, String text2, String text3, String state, String description){ 
     mText1 = text1; 
     mText2 = text2; 
     mText3 = text3; 
     mState = state; 
     mDescription = description; 
    } 

    public String getmText1() {return mText1;} 
    public void setmText1(String mText1) { 
     this.mText1 = mText1; 
    } 
    public String getmText2() { 
     return mText2; 
    } 
    public void setmText2(String mText2) { 
     this.mText2 = mText2; 
    } 
    public String getmText3() { 
     return mText3; 
    } 
    public void setmText3(String mText3) { 
     this.mText3 = mText3; 
    } 
    public String getmState() { 
     return mState; 
    } 
    public void setmState(String mState) { 
     this.mState = mState; 
    } 
    public String getDescription() { 
     return mDescription; 
    } 
    public void setDescription(String mDescription) { 
     this.mDescription = mDescription; 
    } 
} 

答えて

0

あなたはPauzeと行XMLSで二ViewHolderを作成する必要があります。そしてItemViewTypeでは、データソースからの値とそのInflate固有のViewHolderに基づいてテキストの値をチェックします。

これはいくつかのコードです。

public int getItemViewType(int position) { 
     if (products.size()==0){ 
      return 0; 
     }else if (viewFormat== Constants.listV){ 
      return Constants.listV; 
     }else if (viewFormat==Constants.gridV){ 
      return Constants.gridV; 
     } 
     return super.getItemViewType(position); 
    } 

そしてViewHolder:

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     if (viewType== Constants.gridV){ 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items,parent,false); 
      return new MovieViewHolder(view); 
     }else if (viewType==Constants.listV) 
     { 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_item_view,parent,false); 
      return new OtherViewHolder(view); 
     }else{ 
      return null; 
     } 
    } 
関連する問題