android – RecyclerView Adapter在错误的地方重复值

android – RecyclerView Adapter在错误的地方重复值,第1张

概述我正在显示17个CardView. 我正在使用RecyclerView来实现同样的目标. 每个CardView都显示一个通用数据(格式). 根据从JSON文件接收的数据,卡可能会显示一些其他行. .下图显示了2个显示附加数据的CardView(清晰对焦),其余显示公共数据. 看来是对的.但是当我向下滚动到View的底部时,最后一个CardView不会显示任何行,但它会从我们在上面链接中看到的2个中 我正在显示17个CardVIEw.

我正在使用RecyclerVIEw来实现同样的目标.

每个CardVIEw都显示一个通用数据(格式).

根据从JSON文件接收的数据,卡可能会显示一些其他行.

.下图显示了2个显示附加数据的CardVIEw(清晰对焦),其余显示公共数据.

看来是对的.但是当我向下滚动到VIEw的底部时,最后一个CardVIEw不会显示任何行,但它会从我们在上面链接中看到的2个中复制一个并显示. [一次只能发布2个链接]

然后我滚动到顶部,它从两个正确显示的CardVIEw中复制行,并分别在这里重复前2个卡:

我尝试了什么?

>如果,否则

许多类似的问题显示了一个共同的解其中一个答案就在这里.事情是我不知道适用的条件和地点.这是我的代码:

@OverrIDe    public voID onBindVIEwHolder(DataObjectHolder holder,final int position) {        HashMap<Integer,ShopPingCartSummaryObject> dataItem = new HashMap<Integer,ShopPingCartSummaryObject>();         dataItem = mDataset.get(position);        TextVIEw textVIEwComment = holder.textVIEwComment;        ShopPingCartSummaryObject obj = new ShopPingCartSummaryObject();        obj = (ShopPingCartSummaryObject) dataItem.get(position);        holder.textVIEwPackageType.setText(obj.getPackageType());        holder.textVIEwPackageCode.setText(obj.getPackageCode());        holder.textVIEwPackageDesc.setText(obj.getPackageDesc());        params = new relativeLayout.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT);        try {            for (int i = 0; i < obj.getArrayListPart().size(); i++) {                VIEw vIEw2 = LayoutInflater.from(mContext).inflate(R.layout.detailrow,null);                holder.advPartDesc = (TextVIEw) vIEw2.findVIEwByID(R.ID.advPartDesc);                holder.advPartnum = (TextVIEw) vIEw2.findVIEwByID(R.ID.advPartNum);                cv = holder.cv;                advisoryLayout = holder.advisoryLayout;                empty = holder.empty;                layoutPackageSummary = holder.layoutPackageSummary;                holder.detailRow = (relativeLayout) vIEw2.findVIEwByID(R.ID.detailRow);                rallyLayout = new relativeLayout(mContext);                rallyLayout.addVIEw(holder.detailRow);                cnt+=1;                rallyLayout.setID(cnt);                holder.layoutpartList.addVIEw(rallyLayout);                if (i == 0) { //for adding the first row below standard data                    params.addRule(relativeLayout.BELOW,empty.getID() );       holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);                rallyLayout.setLayoutParams(params);            } else { //for more than one rows,2nd row appearing below 1st and 3rd below 2nd and so on                params = new relativeLayout.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT);                params.addRule(relativeLayout.BELOW,cnt - 1);                holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);                holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);                rallyLayout.setLayoutParams(params);            }        }    } catch (Exception e) {        e.printstacktrace();    }}

这就是我尝试从共享链接实现答案的方式:

if(obj.getArrayListPart().size()>0) { /*added the if() condition*/                    if (i == 0) {                        params.addRule(relativeLayout.BELOW,empty.getID());                        holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);          holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);                    rallyLayout.setLayoutParams(params);                } else {                    params = new relativeLayout.LayoutParams(VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT);                    params.addRule(relativeLayout.BELOW,cnt - 1);                    holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);                    holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);                    rallyLayout.setLayoutParams(params);                }            }            else            {                holder.advPartnum.setText("abc");                holder.advPartDesc.setText("abc");            }        }    } catch (Exception e) {        e.printstacktrace();    }

我还补充说:

@OverrIDe    public long getItemID(int position) {        return super.getItemID(position);    }

没有任何效果.

我检查了JsON数据,从列表中删除了一个数组,无论我删除了哪个卡,它还会影响从顶部/底部行(cardvIEws)中删除重复行值,具体取决于卡片之间的卡片与额外的细节和顶部/底部的卡片.还尝试过:

mAdapter.setHasStableIDs(true);

但这只是跳过了普通标准下面出现的所有其他行数据.

请帮助.我提供了我尝试过的任何东西.

解决方法 这是您要求xml的源代码

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.RecyclerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    androID:ID="@+ID/recycler_vIEw"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    app:stackFromEnd="true" />

活动

import androID.graphics.color;import androID.graphics.drawable.Drawable;import androID.graphics.drawable.GradIEntDrawable;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import java.util.ArrayList;import java.util.List;import java.util.Random;import in.ashish29agre.calendar.R;public class RecyclerVIEwActivity extends AppCompatActivity {    private List<Message> messages;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_recycler_vIEw);        MessagesAdapter adapter = initDefaultsAndGetADapter();        RecyclerVIEw recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recycler_vIEw);        recyclerVIEw.setLayoutManager(new linearlayoutmanager(this));        recyclerVIEw.setAdapter(adapter);    }    private MessagesAdapter initDefaultsAndGetADapter() {        GradIEntDrawable fromDrawable = new GradIEntDrawable();        fromDrawable.setcolor(color.parsecolor("#EC407A"));        fromDrawable.setCornerRadius(16f);        GradIEntDrawable toDrawable =  new GradIEntDrawable();        toDrawable.setCornerRadius(16f);        toDrawable.setcolor(color.parsecolor("#64B5F6"));//        toDrawable.getPaint().setStyle(Paint.Style.FILL_AND_stroke);//        toDrawable.getPaint().setstrokeWIDth(1);        messages = new ArrayList<>();        Random random = new Random();        Message message;        for (int i = 0; i < 20; i++) {            message = new Message();            int value = random.nextInt(2);            if (value == Message.TYPE_FROM) {                message.setMsgType(value);                message.setContent("Other user says #" + i);                message.setDrawable(fromDrawable);            } else if (value == Message.TYPE_TO) {                message.setMsgType(value);                message.setContent("You are saying #" + i);                message.setDrawable(toDrawable);            } else {                Log.e("TAG","Oops");            }            messages.add(message);        }        return new MessagesAdapter(messages);    }    private class MessagesAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> {        private List<Message> messages;        public MessagesAdapter(List<Message> messages) {            this.messages = messages;        }        @OverrIDe        public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) {            if (vIEwType == Message.TYPE_FROM) {                VIEw fromLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.from_layout_item,parent,false);                return new OtherUserMessageHolder(fromLayout);            } else if (vIEwType == Message.TYPE_TO) {                VIEw tolayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_layout_item,false);                return new CurrentUserMessageHolder(tolayout);            } else {                throw new IllegalArgumentException("InvalID VIEw type");            }        }        @OverrIDe        public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder,int position) {            if (holder instanceof OtherUserMessageHolder) {                configureOtherUserMessageHolder(holder,position);            } else {                configureCurrentUserMessageHolder(holder,position);            }        }        private voID configureCurrentUserMessageHolder(RecyclerVIEw.VIEwHolder holder,int position) {            if (holder != null && holder instanceof CurrentUserMessageHolder) {                CurrentUserMessageHolder currentUserMessageHolder = (CurrentUserMessageHolder) holder;                currentUserMessageHolder.getToContentTv().setText(messages.get(position).getContent());                currentUserMessageHolder.getToContentTv().setBackground(messages.get(position).getDrawable());            }        }        private voID configureOtherUserMessageHolder(RecyclerVIEw.VIEwHolder holder,int position) {            if (holder != null && holder instanceof OtherUserMessageHolder) {                OtherUserMessageHolder otherUserMessageHolder = (OtherUserMessageHolder) holder;                otherUserMessageHolder.getFromContentTv().setText(messages.get(position).getContent());                otherUserMessageHolder.getFromContentTv().setBackground(messages.get(position).getDrawable());            }        }        @OverrIDe        public int getItemCount() {            return messages.size();        }        @OverrIDe        public int getItemVIEwType(int position) {            return messages.get(position).getMsgType();        }        private final class CurrentUserMessageHolder extends RecyclerVIEw.VIEwHolder {            private TextVIEw toContentTv;            public CurrentUserMessageHolder(VIEw itemVIEw) {                super(itemVIEw);                toContentTv = (TextVIEw) itemVIEw.findVIEwByID(R.ID.to_item_tv);            }            public TextVIEw getToContentTv() {                return toContentTv;            }        }        private final class OtherUserMessageHolder extends RecyclerVIEw.VIEwHolder {            private TextVIEw fromContentTv;            public OtherUserMessageHolder(VIEw itemVIEw) {                super(itemVIEw);                fromContentTv = (TextVIEw) itemVIEw.findVIEwByID(R.ID.from_item_tv);            }            public TextVIEw getFromContentTv() {                return fromContentTv;            }        }    }    private class Message {        private static final int TYPE_FROM = 0;        private static final int TYPE_TO = 1;        private int msgType;        private CharSequence content;        private Drawable drawable;        public int getMsgType() {            return msgType;        }        public voID setMsgType(int msgType) {            this.msgType = msgType;        }        public CharSequence getContent() {            return content;        }        public voID setContent(CharSequence content) {            this.content = content;        }        public Drawable getDrawable() {            return drawable;        }        public voID setDrawable(Drawable drawable) {            this.drawable = drawable;        }    }}
总结

以上是内存溢出为你收集整理的android – RecyclerView Adapter在错误地方重复值全部内容,希望文章能够帮你解决android – RecyclerView Adapter在错误的地方重复值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123681.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存