Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView with one static card and other dynamic cards

I need to do this: a RecycleView with CardView. The first card is static: it show "Details for this order". The other cards after the first are dynamics. So I decided to do this code:

public class DocumentTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{



private List<DocumentType> document; //lista di card da riempire
private Context context;


public DocumentTypeAdapter(List<DocumentType>document, Context context)
{
    this.document = document;
    this.context = context;
}


public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{

    View view;

    if(viewType == 0)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
        ViewSimple simpleView = new ViewSimple(view);
        return simpleView;
    }
    else
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
        DocumentTypeViewHolder documentTypeViewHolder = new DocumentTypeViewHolder(view);
        return documentTypeViewHolder;
    }

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{

    DocumentType documents = document.get(position);

    if(getItemViewType(position)!=0) 
    {
        holder.title.setText(documents.getTitle());
        holder.cert.setText(documents.getTypeofCertificate());
        holder.lastmod.setText(documents.getLastModified());
    }

}

@Override
public int getItemCount()
{
    return document.size();
}

@Override
public int getItemViewType(int position)
{
    return document.size();
}

private class ViewSimple extends RecyclerView.ViewHolder
{
    public ViewSimple(View itemView)
    {
        super(itemView);
    }
}


public class DocumentTypeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    TextView title, lastmod,cert;

    public DocumentTypeViewHolder(View itemView)
    {
        super(itemView);
        title = (TextView)itemView.findViewById(R.id.tipo);
        lastmod = (TextView)itemView.findViewById(R.id.ultimamodifica);
        cert = (TextView)itemView.findViewById(R.id.certificato);

        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {


    }
}
}

but it doesn't work. My question are:

  1. how to make the first card static and other dynamics?
  2. my data are in document list. So how to say to method getItemViewType() that the first card is static and others are generated from the size of the list document?

Edit: this is the code with changes:

public class DocumentTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{


private static final int STATIC_CARD = 0;
private static final int DYNAMIC_CARD = 1;
private List<DocumentType> document; 
private Context context;


public DocumentTypeAdapter(List<DocumentType>document, Context context)
{
    this.document = document;
    this.context = context;
}


public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View view;

    if(viewType == STATIC_CARD)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info, parent, false);
        ViewSimple simpleView = new ViewSimple(view);
        return simpleView;
    }
    else
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_card_type, parent, false);
        DocumentTypeViewHolder documentTypeViewHolder = new DocumentTypeViewHolder(view);
        return documentTypeViewHolder;
    }

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{

    DocumentType documents = document.get(position);

    if(getItemViewType(position)==DYNAMIC_CARD)
    {
        DocumentTypeViewHolder mHolder = (DocumentTypeViewHolder)holder;
        mHolder.data.setText(documents.getData());
        mHolder.status.setText(documents.getStatus());

        Picasso.with(context).load(documents.getImage().toString()).into(mHolder.image, new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {

                Log.d("Picasso","success");
            }

            @Override
            public void onError()
            {

                Log.d("Picasso","error");

            }
        });

    }

}

@Override
public int getItemCount()
{
    return document.size();
}

@Override
public int getItemViewType(int position)
{
   if(position == 0)
    return STATIC_CARD;

   else
    return DYNAMIC_CARD;


}

private class ViewSimple extends RecyclerView.ViewHolder
{
    public ViewSimple(View itemView)
    {
        super(itemView);
    }
}


public class DocumentTypeViewHolder extends RecyclerView.ViewHolder
{
    TextView data, status;
    ImageView image;

    public DocumentTypeViewHolder(View itemView)
    {
        super(itemView);

         data = (TextView)itemView.findViewById(R.id.dateCharging);
         status =(TextView)itemView.findViewById(R.id.statusCharging);
         image = (ImageView)itemView.findViewById(R.id.documentImage);

    }


}
}

Thanks for your answers

like image 398
ste9206 Avatar asked May 23 '16 08:05

ste9206


2 Answers

You should override getItemViewType() and make it return a different value when it should be a dynamic card or when its a static card. Since you want the first card to be static you should return a different value when position == 0. This will look something like this:

    private static final int STATIC_CARD = 0;
    private static final int DYNAMIC_CARD = 1;

    @Override
    public int getItemViewType(int position) {
        if(position == 0) {
            return STATIC_CARD;
        } else {
            return DYNAMIC_CARD;
        }
    }

Then in your onCreateViewHolder() method you should check the viewType and based on the outcome you should inflate a View, which you were already doing OK, but I would replace the hardcoded 0 with the private static final int STATIC_CARD.

Edit: Just came to my mind, if you only need one static card, you might want to consider placing a CardView in your Fragment/Activity xml layout and place the rest of the dynamic cards in a RecyclerView below that static card.

like image 54
Jeffalee Avatar answered Oct 17 '22 12:10

Jeffalee


I'll suggest you to use a library that already correctly implement/handles all that for you: https://github.com/eyeem/RecyclerViewTools

Full disclosure: I wrote the library

Just write your adapter for the dynamic items and then:

DocumentTypeAdapter adapter = new // your adapter here
WrapAdapter wrapAdapter = new WrapAdapter(adapter); // that's from the library
wrapAdapter.addHeader( /* add here your static view */ );
recyclerView.setAdapter(wrapAdapter);

and add those to your build.gradle

repositories {
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

dependencies {
    compile 'com.eyeem.recyclerviewtools:library:0.0.3-SNAPSHOT@aar'
}
like image 2
Budius Avatar answered Oct 17 '22 13:10

Budius