1

I have a problem regarding RecyclerView duplicating its items on scroll to top.

My RecyclerView populates its View by taking values from an online database on scroll down to a certain threshold. I am well aware of RecyclerView's behavior on reusing the Views, but I don't want that to happen as it'll get confusing due to having Views with different items inside.

I've searched around SO for the solution. Some said that I have to override getItemId like :

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

But they don't elaborate more on that.

Tried using setHasStableIds(true); but it's not working. When I scroll down to populate the RecyclerView, then scroll quickly back up, the first item still shows the last item I scrolled to, or any other random item.

I have this in onBindViewHolder :

if(loading)
{
// Do nothing
}
else {
((ObjectViewHolder) holder).progressBar.setVisibility(View.GONE);
            ((ObjectViewHolder) holder).postListWrapper.setVisibility(View.VISIBLE);
            Uri userImageUri = Uri.parse(mDataset.get(position).author_avatar);

...
// The rest of the code
}

Does it have to do with the error I'm getting? The loading is changed to false when the Fragment containing RecyclerView finished getting value from the database.

Here's the RecyclerView onScrollListener :

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int firstVisibleItem = manager.findFirstCompletelyVisibleItemPosition();
            if(firstVisibleItem < 1 )
            {
                swipeRefreshLayout.setEnabled(true);
            }else
            {
                swipeRefreshLayout.setEnabled(false);
            }
            totalItemCount = manager.getItemCount();
            lastVisibleItem = manager.findLastVisibleItemPosition();
            int visibleThreshold = 2;

            if(isLoading == false)
            {
                if (totalItemCount <= lastVisibleItem + visibleThreshold) {

                    if(lobiAdapter.getItemCount() > 0)
                    {
                        if (lobiAdapter.getItemCount() < 5)
                        {
                            setIsLoaded();
                        }else{
                            // End has been reached
                            // Do something

                            PostListAPI postListAPI = new PostListAPI();
                            postListAPI.query.user_id = userId;
                            postListAPI.query.post_count = String.valueOf(counter);
                            postListAPI.query.flag = "load";

                            NewsFeedsAPIFunc newsFeedsAPIFunc = new NewsFeedsAPIFunc(BottomLobiFragment.this.getActivity());

                            newsFeedsAPIFunc.delegate = BottomLobiFragment.this;
                            setIsLoading();
                            newsFeedsAPIFunc.execute(postListAPI);
                        }
                    }
                    else {
                        setIsLoaded();
                    }
                }
            }
        }
    });
Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
  • on scroll down to a certain threshold? What it is mean?better you will add more code – yshahak Mar 31 '16 at 04:44
  • It's basically an infinite scroll.. I've found the source of the problem, can a whole holder be checked whether it's different from the recycled holder? So each holder is different from the other – Kevin Murvie Mar 31 '16 at 05:06
  • 1
    sorry man, I just not understand your question. What is "whole holder"? and what is "recycled holder"? You have to be more detailed with you intention, I don't know what you have in your head while writing the question... – yshahak Mar 31 '16 at 05:17
  • Wow sorry man, I was in a confusion lol, what I meant by whole holder is the row item in the Recyclerview.. As in item defined by position in the RecyclerView, and RecycledHolder is the view which is reused when we scroll back to top of the RecyclerView.. So basically I wanna check each holder in each position with a certain.. Tag maybe? But there isn't any setTag on it.. I kept getting an image on a supposed to be empty item, or empty item on supposedly.. Not empty item.. – Kevin Murvie Mar 31 '16 at 06:10
  • You need to follow a tutorial on this. You are basically doing everything wrong with a `ListView` and instead of using it's "magic" you are fighting against it. Start at section 4 of this one: http://www.vogella.com/tutorials/AndroidListView/article.html – C0D3LIC1OU5 Mar 31 '16 at 15:21
  • @C0D3LIC1OU5 I'm using `RecyclerView` due to the intricacy of each of the item.. I've revised my question here http://stackoverflow.com/questions/36327049/android-recyclerview-with-one-layout-multiple-setvisibility and gotten result! :D but I want to know what am I doing wrong though.. In which part am I fighting against it?? – Kevin Murvie Apr 01 '16 at 02:13

0 Answers0