简体   繁体   English

在具有多种视图类型的Recycler View中,滚动时重复的项目

[英]In Recycler View with multiple view types items duplicating on scrollup

I have a recyclerview with multiple view types and pagination enabled. 我有一个具有多种视图类型和分页功能的recyclerview。 Scrolling up after two pages the items are not holding the view and are duplicating. 在两页之后向上滚动,这些项目将不保存视图并且正在复制。 I have implemented getItemId return unique id and getViewtype return unique value. 我已经实现了getItemId返回唯一ID和getViewtype返回唯一值。 Also have the the adapter's setHasStableIds to true. 也将适配器的setHasStableIds设置为true。 Looking forward for any help. 期待任何帮助。 Thank you. 谢谢。

getItemId getItemId

if (postList.get(position).getId()== null){
        return position;
    } else{
        return postList.get(position).getId();
    }

getItemViewType getItemViewType

 if (postList.get(position).getPostType().equals("contest")){
            return TYPE_CONTEST;
  } else if(postList.get(position).getPostType().equals("suggestion")){
            return TYPE_SUGGESTION;
        } else{
            return TYPE_POST_POLL;
        }
    } else{
        return TYPE_HEADER;
       }

Try something like this: 尝试这样的事情:

@Override
public int getItemViewType(int position) {
    if (isValidPosition(position)) {
        Dataset d = mDataset[position];
        if ("contest".equals(d.getViewType())) {
            return VIEW_TYPE_CONTEST;
        } else if ("suggestion".equals(d.getViewType())) {
            return VIEW_TYPE_SUGGESTION;
        }
        // all other cases here
    }
    // default type
    return VIEW_TYPE_CONTEST;
}

and then : 接着 :

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Dataset d = mDataset[position];

    if (holder instanceof ContestVH) {
        ContestDataset contest = (ContestDataset) d;
        ContestVH vh = (ContestVH) holder;
        // bind your contest views here
    } else if (holder instanceof SuggestionVH) {
        SuggestionDataset suggestion = (SuggestionDataset) d;
        SuggestionVH vh = (SuggestionVH) holder;
        // bind your suggestion views here
    }
    // all other cases here
}

this way your String view type is your only source of truth. 这样,您的String视图类型就是您唯一的真理来源。

For pagination in recyclerview you should use onScrollListener this 对于在recyclerview中的分页,应该使用onScrollListener

recyclerList.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        boolean isLast = isLastItemDisplaying(recyclerView);  //get last displayed item
        if (loading && isLast) {  //this check if your data is loading and if its last list item list will not load again
            loading = false;

            //get your data according to page count it will refresh your recycler list 

        }
    }
});

because in pagination you can get data from Api on page scroll in recycler view. 因为在分页中,您可以在Recycler视图中的页面滚动中从Api获取数据。 So data will come in chunks according to page count. 因此,数据将根据页数成块出现。 For this behaviour you can use on scroll listener in recycler list and refresh data on scroll of list until last item of list arrived. 为此,您可以在回收商列表中的滚动侦听器上使用,并在列表滚动时刷新数据,直到列表的最后一项到达。 i hope it will help you :) 我希望它将对您有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM