简体   繁体   中英

In Recycler View with multiple view types items duplicating on scrollup

I have a recyclerview with multiple view types and pagination enabled. 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. Also have the the adapter's setHasStableIds to true. Looking forward for any help. Thank you.

getItemId

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

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.

For pagination in recyclerview you should use onScrollListener this

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. 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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