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

时间:2016-05-23 08:07:47

标签: android 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, 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)
    {


    }
}
}

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

  1. 如何使第一张卡静态和其他动态?
  2. 我的数据在document列表中。那么如何对方法getItemViewType()说第一张卡是静态的,而其他卡是从列表document的大小生成的?
  3. 编辑:这是带有更改的代码:

    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);
    
        }
    
    
    }
    }
    

    感谢您的回答

2 个答案:

答案 0 :(得分:8)

你应该覆盖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,并根据结果,您应该膨胀View,您已经做好了,但我会替换硬编码的0使用private static final int STATIC_CARD

修改:我想到,如果您只需要一张静态卡,您可以考虑在CardView / {{1}中添加Fragment } xml布局并将其余动态卡放在该静态卡下面的Activity

答案 1 :(得分:2)

我建议你使用一个已经正确实现/处理所有这些内容的库:https://github.com/eyeem/RecyclerViewTools

完全披露:我写了这个库

只需为动态项目编写适配器,然后:

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);

并将其添加到build.gradle

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

dependencies {
    compile 'com.eyeem.recyclerviewtools:library:0.0.3-SNAPSHOT@aar'
}