Large gap forms between RecyclerView items when scrolling down

I'm making a ToDo list app, and while testing it, for some reason, a huge gap forms between the items whenever I try to scroll down. It always happens whenever I Drag and Drop the items, but I don't see any errors with my ItemTouchHelper adapter and callback class. It would be awesome if you can help me out.

Before:

After:

RecyclerAdapter.java

public class RecyclerAdapter extends                                                                                                                                 

RecyclerView.Adapter<RecyclerAdapter.RecyclerVH> implements ItemTouchHelperAdapter{
private LayoutInflater layoutInflater;
ArrayList<Info> data;
Context context;

public RecyclerAdapter(Context context) {
    layoutInflater = LayoutInflater.from(context);
    this.context = context;
}

public void setData(ArrayList<Info> data) {
    this.data = data;
    notifyDataSetChanged();
}

@Override
public RecyclerVH onCreateViewHolder(ViewGroup viewGroup, int position) {
    View view = layoutInflater.inflate(R.layout.custom_row, viewGroup, false);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("R.A onClick Owen", "onClick method triggered");
        }
    });
    RecyclerVH recyclerVH = new RecyclerVH(view);
    return recyclerVH;
}

@Override
public void onBindViewHolder(RecyclerVH recyclerVH, int position) {
    Log.d("RecyclerView", "onBindVH called: " + position);

    final Info currentObject = data.get(position);
    // Current Info object retrieved for current RecyclerView item - USED FOR DELETE
    recyclerVH.listTitle.setText(currentObject.title);
    recyclerVH.listContent.setText(currentObject.content);

    /*recyclerVH.listTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Open new Activity containing note content
            Toast.makeText(this, "Opening: " + currentObject.title, Toast.LENGTH_LONG).show();
        }
    });*/
}

public void deleteItem(int position) {
    DBInfo dbInfo = new DBInfo(context);
    dbInfo.deleteNote(data.get(position));
    // Deletes RV item/position's Info object

    data.remove(position);
    // Removes Info object at specified position

    notifyItemRemoved(position);
    // Notifies the RV that item has been removed
}


@Override
public int getItemCount() {
    return data.size();
}

// This is where the Swipe and Drag-And-Drog methods come into place
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    // Swapping positions
    // ATTEMPT TO UNDERSTAND WHAT IS GOING ON HERE
    Collections.swap(data, fromPosition, toPosition);
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

@Override
public void onItemDismiss(int position) {
    // Deleting item from RV and DB
    deleteItem(position);
}

class RecyclerVH extends RecyclerView.ViewHolder implements View.OnClickListener{
    // OnClickListener is implemented here
    // Can also be added at onBindViewHolder above
    TextView listTitle, listContent;

    public RecyclerVH(View itemView) {
        super(itemView);

        listTitle = (TextView) itemView.findViewById(R.id.title);
        listContent = (TextView) itemView.findViewById(R.id.content);

        listTitle.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(context, "Opening: Note" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
        // PS NEVER ADD listTitle VARIABLE AS PUBLIC VARIABLE ABOVE WHICH IS GIVEN VALUE AT ONBINDVH
        // THIS IS BECAUSE THE VALUE WILL CHANGE IF ITEM IS ADDED OR DELETED
    }
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:weightSum="1">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_corners" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <com.melnykov.fab.FloatingActionButton
            android:id="@+id/fab_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"
            android:gravity="bottom|end"
            android:onClick="addNote"
            android:src="@drawable/fab_ic_add"
            fab:fab_colorNormal="@color/colorPrimary"
            fab:fab_colorPressed="@color/colorPrimaryDark"
            fab:fab_colorRipple="@color/colorPrimaryDark" />
    </RelativeLayout>
</FrameLayout>
</LinearLayout>

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:padding="8dp"
        android:text="@string/test"
        android:textSize="18sp"
        android:textStyle="bold" />
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/main"
    android:paddingLeft="8dp">
    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/test"
        android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>

Thank you so much to whoever can help me out. I am pulling my hair out as I type.

EDIT: I have confirmed that it is not my ItemTouchHelper class that's the problem. (Tried running without it being called, problem still occurs.) Also, it seems that when a dialog is shown and the keyboard brought up, the RecyclerView in the background resolves the problem by itself. After dialog is removed, the problem repeats (i.e. Scrolling puts massive space between items)

Luksprog's answer to Gabriele Mariotti's answer works.

According to the doc

With the release 2.3.0 there is an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. >This means that previously unavailable scenarios, such as using WRAP_CONTENT >for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

In your item layout you have to change:

android:layout_height="match_parent"

with

android:layout_height="wrap_content" 

RecyclerView in Android Example Best Practices, RecyclerView Android example, I followed best way of RecyclerView implementation. holders when user scroll down RecyclerView reuse the cell and bind with holder. <item>Nike Enters 10-Year Partnership With Major League Baseball! the size and location of offsets between items in the parent * RecyclerView. RecyclerView items with big empty space after 23.2.0. According to the doc. With the release 23.2.0 there is an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents.

change in Recycler view match_parent to wrap_content:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Also change in item layout xml

Make parent layout height match_parent to wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

Mastering Complex Lists with the Android RecyclerView, Valdio Veliu looks at the RecyclerView in Android, perfect for handling The idea of view recycling has been in Android since version 1 in the form of the ListView . The idea is simple: to present a large collection of data using a small As the user scrolls the view, the Layout Manager determines when new  Each row has a play button, textview and Progressbar. when click on the play button have to play audio from my sdcard and have to progress Progressbar The problem is when i scroll down the recyclerview change the Progressbar in next row.means I can fit 5 items on the screen

This happened to me many times!

All you need to do is... make layout_height of row file wrap_content and your problem solved..

android:layout_height="wrap_content"

Happy coding!

Using the RecyclerView · codepath/android_guides Wiki · GitHub, Furthermore, it provides animation support for RecyclerView items whenever they are has the android:divider property for easy dividers between items in the list. LinearLayoutManager shows items in a vertical or horizontal scrolling list. It is recommended however for larger lists that you execute this computation in a  RecyclerView Animations – Moving Items February 9, 2018 February 16, 2018 Mark Allison 1 Comment RecyclerView is a really useful way of displaying content in list form, particularly when the content is dynamic and / or there are large numbers of items.

Its because you are using match_parent in height of root view of the row item in your vertically oriented listview/recyclerview. When you use that the item expands completely wrt to its parent. Use wrap_content for height when the recyclerview is vertically oriented and for width when it is horizantally oriented.

Handling Scrolls with CoordinatorLayout · codepath/android_guides , Sliding the Floating Action Button up and down to make space for the Snackbar. CoordinatorLayout android:id="@+id/main_content" ScrollingViewBehavior describes a dependency between the RecyclerView and AppBarLayout. Set layout_margin of the item view to half the desired spacing between items; Set padding of RecyclerView, also to half the desired spacing between items; Set clipToPadding to false on the RecyclerView - this is very important, otherwise the padding in the previous step cuts off the edges of the scroll view. Tada! All items are uniformly spaced and sized.

i have this problem and i just user

android:layout_height="wrap_content"

for parent of every item.

Fast Scrolling with RecyclerView, A First Glance at Android's RecyclerView Occurs when the overlap in the last row is not large enough to are scrolling down //Check against top bound if (​topBoundReached) { int When I scrolled back, there would be a space (white gap) between where the cell was and where it should have been. Pagination with RecyclerView. Etienne Lawlor. I have chosen a LinearLayoutManger here and set up an ItemAnimator so that items will slide up when added and slide down when removed.

Building a RecyclerView LayoutManager – Part 1, Imagine you have a large data source you plan to load on a RecyclerView Xamarin Android RecyclerView with CardView button clicked multiple times The app Automatic listview scroll down feature mostly sees in chatting android apps the gap between the data in your word list and the RecyclerView that displays it​. In this particular case, the AppBarLayout.ScrollingViewBehavior describes a dependency between the RecyclerView and AppBarLayout. Any scroll events to the RecyclerView should trigger changes to the AppBarLayout layout or any views contained within it. Scroll events in the RecyclerView trigger changes inside views declared within AppBarLayout by

Scroll recyclerview on button click android, This makes ScrollView ideal for scrolling pages of free-form text smoothly, When I'm scrolling down, the items above the RecyclerView does not scroll unless I start a sliding panel menu to navigate between major modules of the application. Because the UI Thread was not doing any work in that gap between frames,  Android RecyclerView is similar to ListView except that it forces us to use RecyclerView.ViewHolder class to hold the elements which is not a compulsion in ListView. As the name suggests, Android RecyclerView is used to reuse cells when scrolling up and down by recycling the items in the list.

Recyclerview not scrolling smoothly android, When I pull down my page, the refresh icon appears which is great. But my actual cards (within the RecyclerView) don't move down the page (like they do on Facebook,Twitter etc. on pull down Refresh) - a gap is not placed between the refresh icon and the first card (unlike on facebooke etc.)

Comments
  • This probably is not about your code. It's about your layouts. Show the layouts you use here.
  • @DougStevenson I highly doubt it would be my layouts though, as before this problem occurred, I didn't touch them at all. However, I will still be grateful for any insight you can provide.
  • maybe this is the cause stackoverflow.com/questions/35728179/…
  • @Luksprog OMG YOU SAVED MY LIFE THANK YOU THANK YOU
  • Remove Match_parent with in the height of relative layout in the custom.XML file.
  • as @mohit said, we also need to have the parent layout of item_layout as: xml android:layout_height="wrap_content" otherwise it won't work
  • if that doesn't work then your recyclerview layout file android:layout_height="wrap_content"