Home » Android » scroll – Android RecyclerView Duplicate Item When Scrolling

scroll – Android RecyclerView Duplicate Item When Scrolling

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a problem in RecyclerView. When I move item in RV and then scroll, saw some items has duplicated.

How to&Answers:

RecyclerView will recycle the view.When you delete data,call notifyItemChanged(pos)or notifyDataSetChanged() method.

Answer:

I know its late but hope it will help someone. Override these two methods in your adapter.

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

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

Answer:

It is your notifyDataSetChanged() that is the issue.

Check that you used it properly.

That is:

private void parseJsonFeed(JSONArray response) {

for (int i = 0; i < response.length(); i++)
        try {
            JSONObject obj = response.getJSONObject(i);
            MyData myData = new MyData();
            myData.setContent_title(obj.getString("content_title"));
            ...
            ...
            ...
            ...
            // adding content to array
            homeList.add(myData);
              } catch (JSONException e) {
            e.printStackTrace();
        }
    //Notifying the adapter that data has been added or changed
   //this must always be called else the recycler would not understand when to stop or start working.
    recyclerViewAdapter.notifyDataSetChanged();
   }

Answer:

I think I’m late in here but anyway I’ll suggest a way that worked good for me, maybe someone still facing problems with this..
So, I added my recyclerview inside a nestedScrollView then disabled nested scrolling for my recyclerview.

Using this method the scrolling will be detected by the nestedScrollView, and the recyclerview stopped duplicating items while scrolling.

That’s my xml code:

<androidx.core.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <androidx.recyclerview.widget.RecyclerView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:nestedScrollingEnabled="false"/>

</androidx.core.widget.NestedScrollView>