滚动回收站视图时如何防止项目重复

时间:2015-10-24 09:57:10

标签: android android-listview android-recyclerview layout-inflater

我在Recycler视图内部创建了一行,我在内部膨胀了两行或更多但是当我滚动时,项目再次被使用。 我没有得到回收视图的位置或动态删除它们 我需要一个提要和他们的评论。对于评论我需要膨胀布局,以便在创建了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) 
        }


    }
}

5 个答案:

答案 0 :(得分:83)

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

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}

答案 1 :(得分:3)

在适配器类AllFeedsAdapter中,您需要覆盖方法:

@Override
public long getItemId(int position){
// return specific item's id here
}

答案 2 :(得分:0)

为我工作了在将数据加载到Recycler View之前,请清除内容

listOftrailers.clear();

答案 3 :(得分:-1)

我也有这个问题。我可以确认添加getItemId和getItemViewType到适配器在滚动时解决了recyclerview项目中的图像重复。

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}

我会将此作为评论,但我的反馈少于5条。

答案 4 :(得分:-2)

onBindViewHolder方法结束时,请添加:holder.itemView.setTag(arrayListAllFeedsData.get(position));

相关问题