-1

Hey guys I'm implementing two cards in an recyclerview and also created two view holder but didn't get the code for adapter help me guys.

public class ViewHolder1 extends RecyclerView.ViewHolder {

    private TextView Chatin;

    public ViewHolder1(View v) {
        super(v);
        Chatin = (TextView) v.findViewById(R.id.Chatin);
    }

    public TextView getChatin() {
        return Chatin;
    }

    public void setChatin(TextView chatin) {
        this.Chatin = chatin;
    }


}

Other view holder is same as it is.

Shashank Verma
  • 284
  • 4
  • 18
  • Please refer the url http://stackoverflow.com/questions/25914003/recyclerview-and-handling-different-type-of-row-inflation might be helpful. – Sachit karki Nov 18 '16 at 05:04

2 Answers2

3

You can display multiple cards in Recycler view.

1) Override getItemViewType() method

@Override
public int getItemViewType(int position) {

    switch (position) {
        case 0:
            return VIEW_TYPE_ONE;
        case 1:
            return VIEW_TYPE_TWO;
             .
             .
             .
        case n:
            return VIEW_TYPE_N;

        default:
            return DEFAULT_VIEW_TYPE;
    }
}

2)Check for viewType in onCreateViewHolder() method

 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   switch (viewType) {
     case  VIEW_TYPE_ONE: 
      // return card one.  
          .
          .
          .
    }
 }

3) Check for view type in onBindViewHolder() with the position and set data to your card accordingly.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     switch (getItemViewType(position)) {
        case VIEW_TYPE_ONE:
          ViewTypeOneViewHolder holderObj= (ViewTypeOneViewHolder) holder;
          //Your implementation for view type one.

          break;
          .
          .
          .
     }
 }
Gopal
  • 1,452
  • 1
  • 9
  • 28
2

Yes we can use multiple cardView. For reference see below link for Sample Adapter :-https://github.com/subbuboyapati/MovieMasti/blob/master/app/src/main/java/com/subbu/moviemasti/adapter/ReviewAdapter.java

This answer also help you. Can I Use Only One RecyclerView For The Three Different CardView With Different Design?

Have a look at this tutorial also, http://arjunu.com/2015/10/android-recyclerview-with-different-cardviews/ .

Community
  • 1
  • 1
Priya Jagtap
  • 973
  • 12
  • 18