滚动回收站视图时如何防止项目重复
Posted
技术标签:
【中文标题】滚动回收站视图时如何防止项目重复【英文标题】:How to prevent items from getting duplicated when scrolling recycler view 【发布时间】:2016-01-23 20:17:57 【问题描述】:我在回收站视图中创建了一行,在里面我已经膨胀了两行或更多行,但是当我滚动时,这些项目又被使用了。 我没有在哪里回收视图或动态删除它们 我需要一个提要和他们的 cmets。对于 cmets,我需要膨胀布局以在为其创建 getCommentInflaterView 方法的提要下方显示它们。
但是使用 getCommentInflaterView() 创建的视图被复制了。 我的适配器类:
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)
【问题讨论】:
【参考方案1】:在适配器中重写这两个方法。
@Override
public long getItemId(int position)
return position;
@Override
public int getItemViewType(int position)
return position;
【讨论】:
经过近 1 小时的搜索和尝试。非常感谢 有效!但我不知道为什么。你能解释一下吗? @ChinglimCHAN 这与android处理没有id的项目的方式有关。只需按住 ctrl(mac 上的 cmd)按下函数名称即可。您将执行此操作两次并显示: /** * 返回position
处项目的稳定 ID。如果 #hasStableIds() * 将返回 false,则此方法应返回 #NO_ID。此方法的默认实现 * 返回 NO_ID。 * * position 适配器位置要查询 * 位置的item的稳定ID */
@Prasad P 这很好用,但如果我的 recyclerView 中需要视图类型怎么办?
@Skullper 这不适用于多种视图类型,有什么解决方案吗?【参考方案2】:
在您的适配器类AllFeedsAdapter
中,您需要重写该方法:
@Override
public long getItemId(int position)
// return specific item's id here
【讨论】:
请具体说明您想要什么?看看这个如何提问***.com/help/how-to-ask【参考方案3】:为我工作 在 Recycler View 中加载数据之前清除内容
listOftrailers.clear();
【讨论】:
【参考方案4】:在onBindViewHolder
方法的末尾,包括:holder.itemView.setTag(arrayListAllFeedsData.get(position));
【讨论】:
以上是关于滚动回收站视图时如何防止项目重复的主要内容,如果未能解决你的问题,请参考以下文章