android– 滚动回收器视图时如何防止项目重复

android– 滚动回收器视图时如何防止项目重复,第1张

概述我在回收器视图内部创建了一行,我在内部膨胀了两行或更多但是当我滚动时,项目再次被使用.我没有得到回收视图的位置或动态删除它们我需要一个提要和他们的评论.对于评论我需要膨胀布局,以便在创建了getCommentInflaterView方法的提要下面显示它们.但是使用getCommentInflaterView

我在回收器视图内部创建了一行,我在内部膨胀了两行或更多但是当我滚动时,项目再次被使用.
我没有得到回收视图的位置或动态删除它们
我需要一个提要和他们的评论.对于评论我需要膨胀布局,以便在创建了getCommentInflaterVIEw方法的提要下面显示它们.

但是使用getCommentInflaterVIEw()创建的视图会重复.
MyAdapterClass:

public class AllFeedsAdapter extends        RecyclerVIEw.Adapter<AllFeedsAdapter.VIEwHolder> {    ArrayList<Data> arrayListAllFeedsData;    Comment objComment;    Context context;    public AllFeedsAdapter(Context context, ArrayList<Data> data) {        this.context = context;        arrayListAllFeedsData = new ArrayList<>();        arrayListAllFeedsData = data;    }    @OverrIDe    public AllFeedsAdapter.VIEwHolder onCreateVIEwHolder(VIEwGroup parent, int vIEwType) {        Context context = parent.getContext();        LayoutInflater inflater = LayoutInflater.from(context);        // Inflate the custom layout        VIEw post_row_vIEw = inflater.inflate(R.layout.row_teacher_post, parent, false);        // Return a new holder instance        VIEwHolder vIEwHolder = new VIEwHolder(post_row_vIEw);        return vIEwHolder;    }    @OverrIDe    public voID onBindVIEwHolder(AllFeedsAdapter.VIEwHolder holder, int position) {        holder.txtUsernamePostCreator.setText(arrayListAllFeedsData.get(position).getFull_name());        holder.txtPostContent.setText(arrayListAllFeedsData.get(position).getFeed_text());        holder.txtPostlikeCounter.setText(arrayListAllFeedsData.get(position).getTotal_like());        holder.txtPostCommentsCounter.setText(arrayListAllFeedsData.get(position).getTotal_comment());        if (arrayListAllFeedsData.get(position).getComment().size() > 0) {            if (arrayListAllFeedsData.get(position).getComment().size() > 2) {                for (int i = 0; i < 2; i++) {                    objComment = arrayListAllFeedsData.get(position).getComment().get(i);                    if (getCommentInflaterVIEw(objComment) == null) {                        holder.llCommentRowInflater.addVIEw(getCommentInflaterVIEw(objComment));                    } else {                        holder.llCommentRowInflater.removeAllVIEws();                    }                }            } else {                for (int i = 0; i < arrayListAllFeedsData.get(position).getComment().size(); i++) {                    objComment = arrayListAllFeedsData.get(position).getComment().get(i);                    if (getCommentInflaterVIEw(objComment) == null) {                        holder.llCommentRowInflater.addVIEw(getCommentInflaterVIEw(objComment));                    } else {                        holder.llCommentRowInflater.removeAllVIEws();                    }                }            }        }    }    private VIEw getCommentInflaterVIEw(Comment commentData) {        LayoutInflater layoutInflater = LayoutInflater.from(context);        VIEw v = layoutInflater.inflate(R.layout.post_comments_List_item, null, false);        TextVIEw txtCommenterUsername = (TextVIEw) v.findVIEwByID(R.ID.txt_username_commenter);        TextVIEw txtCommenterComment = (TextVIEw) v.findVIEwByID(R.ID.txt_comments_from_commenter);        TextVIEw txtCommentDuration = (TextVIEw) v.findVIEwByID(R.ID.txt_comment_duration);        txtCommenterUsername.setText(commentData.getUsername());        txtCommenterComment.setText(commentData.getComment());        txtCommentDuration.setText(commentData.getCommentBy());        return v;    }    @OverrIDe    public int getItemCount() {        return arrayListAllFeedsData.size();    }    /**     * USed to create static class for all the vIEw if Listitem child     */    public static class VIEwHolder extends RecyclerVIEw.VIEwHolder {        // Your holder should contain a member variable        // for any vIEw that will be set as you render a row        public linearLayout llParentTeacherPost, llCommentRowInflater;        // We also create a constructor that accepts the entire item row        // and does the vIEw lookups to find each subvIEw        public VIEwHolder(VIEw itemVIEw) {            // Stores the itemVIEw in a public final member variable that can be used            // to access the context from any VIEwHolder instance.            super(itemVIEw);            llParentTeacherPost = (linearLayout) itemVIEw.findVIEwByID(R.ID.ll_parent_teacher_post);            llCommentRowInflater = (linearLayout) itemVIEw.findVIEwByID(R.ID.ll_comment_row_inflater);            imgDpPostCreator = (ImageVIEw) itemVIEw.findVIEwByID(R.ID.img_dp_post_creator);            txtUsernamePostCreator = (TextVIEw)         }    }}

解决方法:

在适配器中覆盖这两个方法.

@OverrIDepublic long getItemID(int position) {    return position;}@OverrIDepublic int getItemVIEwType(int position) {   return position;}
总结

以上是内存溢出为你收集整理的android – 滚动回收器视图时如何防止项目重复全部内容,希望文章能够帮你解决android – 滚动回收器视图时如何防止项目重复所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1100699.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存