RecyclerView with one static card and other dynamic cards

纵饮孤独 提交于 2019-12-12 08:00:50

问题


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


回答1:


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.




回答2:


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'
}


来源:https://stackoverflow.com/questions/37385756/recyclerview-with-one-static-card-and-other-dynamic-cards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!