android – RecyclerView带有一张静态卡和其他动态卡

前端之家收集整理的这篇文章主要介绍了android – RecyclerView带有一张静态卡和其他动态卡前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要这样做:使用CardView的RecycleView.第一张卡是静态的:它显示“此订单的详细信息”.第一个之后的其他卡是动态的.所以我决定做这个代码
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,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)
    {


    }
}
}

但它不起作用.
我的问题是:

>如何使第一张卡静态和其他动态?
>我的数据在文档列表中.那么如何对方法getItemViewType()说第一张卡是静态的,其他卡是从列表文档的大小生成的?

编辑:这是带有更改的代码

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,int viewType)
{
    View view;

    if(viewType == STATIC_CARD)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_info,false);
        ViewSimple simpleView = new ViewSimple(view);
        return simpleView;
    }
    else
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_card_type,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);

    }


}
}

谢谢你的回答

解决方法

您应该覆盖getItemViewType()并使其在应该是动态卡或静态卡时返回不同的值.由于您希望第一张卡是静态的,因此当位置== 0时,您应该返回不同的值.这看起来像这样:
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;
        }
    }

然后在你的onCreateViewHolder()方法中,你应该检查viewType并根据你应该给视图充气的结果,你已经做好了,但我会用私有静态最终的int STATIC_CARD替换硬编码的0.

编辑:刚出现在我的脑海中,如果您只需要一张静态卡,您可能需要考虑在Fragment / Activity xml布局中放置一个CardView,并将其余的动态卡放在该静态卡下面的RecyclerView中.

原文链接:https://www.f2er.com/android/310192.html

猜你在找的Android相关文章