Search code examples
javaandroid-studioandroid-recyclerviewandroid-listviewandroid-adapter

RecyclerView shuffles items and changing values on scrolling fast in adapter, Android Studio


I am using RecyclerView to place JSON data, and in adapter, I set some conditions that specific values have different values then hide the RelativeLayout.

On the app run, everything works fine But when I scroll the data, the hidden or invisible field gets visible.

Now I want the solution to this problem, that when I scroll the RecyclerView, the data should be in its defined state instead of shuffling or changing its state because of its position.


Solution

  • The solution is very simple, here just have to override two methods in Adapter class and it will solve the issue.

    The reason is because some other overridden methods need to call these override methods while rendering the list items.

    Place this code in the end of adapter class. As Example: https://i.sstatic.net/Q81Ud.png

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