RoCk February 2016

Can I Use Only One RecyclerView For The Three Different CardView With Different Design?

Can I create only one recycler_view.xml to handle my three set of cardview.xml with different design? Or Do I need to create separate recyclerView for each one of my cardViews.

For example I have different CardView for News, Activities and Groups.

Answers


N J February 2016

Yes you can use getItemViewType()

Depending upon view type inflate different layout for item

Example

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
    }

    @Override
    public int getItemViewType(int position) {

        return viewType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }
}


subrahmanyam boyapati February 2016

Yes we can use multiple cardView. For reference see below link Sample Adapter

Here i am showing list of reviews when no reviews i am displaying no reviews availble. Here i am creating 2 ViewHolders one for my review item and another one for no review available item.

code:

package com.subbu.moviemasti.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.subbu.moviemasti.R;
import com.subbu.moviemasti.entities.Review;
import com.subbu.moviemasti.fragment.IReviewView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by subrahmanyam on 24-12-2015.
 */
public class ReviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final List<Review> results;

    public ReviewAdapter(IReviewView reviewFragment, List<Review> results) {
        this.results = results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                view = View.inflate(parent.getContext(), R.layout.list_item_empty_view, null);
                viewHolder = new EmptyViewHolder(view);
                break;
            case 1:
                view = View.inflate(parent.getContext(), R.layout.review_list_item, null);
                viewHolder = new ViewHolder(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case 0:
                EmptyView 

Post Status

Asked in February 2016
Viewed 3,312 times
Voted 7
Answered 2 times

Search




Leave an answer