public class documentTypeAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder>{private List<documentType> document; //Lista di card da rIEmpireprivate 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; }}@OverrIDepublic 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()); }}@OverrIDepublic int getItemCount(){ return document.size();}@OverrIDepublic 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"); } }); }}@OverrIDepublic int getItemCount(){ return document.size();}@OverrIDepublic 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中.
总结以上是内存溢出为你收集整理的android – RecyclerView带有一张静态卡和其他动态卡全部内容,希望文章能够帮你解决android – RecyclerView带有一张静态卡和其他动态卡所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)