Fix RecyclerView not being smooth while inside a NestedScrollView

use

recyclerView.setNestedScrollingEnabled(false);
keep in mind this will cause the RecyclerView not to use the ViewHolder pattern. Use it when there aren’t a lot of items in it

Adding dividers to a RecyclerView and choosing which ones to show (Custom RecyclerView.ItemDecoration)

Most of the tutorials I found online say to override the “getItemOffsets” method and set the outRect.setEmpty(); on the view you don’t want a divider. This doesn’t seem to work, at least when using the “com.android.support:recyclerview-v7:25.3.1”.

To make it work i had to override the onDraw method. See below code for a custom  ItemDecoration class

 

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

import uk.co.travelrepublic.travelrepublic.util.CompatUtil;

public class DividerItemDecorationWidget extends RecyclerView.ItemDecoration {
    private final Paint mPaint;

    public DividerItemDecorationWidget(Context context, @ColorRes int color, float heightDp) {
        mPaint = new Paint();
        mPaint.setColor(ContextCompat.getColor(context, color));
        final float thickness = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, heightDp, context.getResources().getDisplayMetrics());
        mPaint.setStrokeWidth(thickness);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        // we set the stroke width before, so as to correctly draw the line we have to offset by width / 2
        final int offset = (int) (mPaint.getStrokeWidth() / 2);

        // this will iterate over every visible view except the last one, we don't want a divider in the last view
        for (int i = 0; i < (parent.getChildCount() - 1); i++) {
            final View view = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

            final int position = params.getViewAdapterPosition();

            if (position < state.getItemCount()) {
                c.drawLine(view.getLeft(), view.getBottom() + offset, view.getRight(), view.getBottom() + offset, mPaint);
            }
        }
    }
}

 

and if you want the divider lines to be drawn on top of the views (this is needed if you have a background color on the views, if this is not used then the divider lines will be hidden behind the background color)

 

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

    // we want to retrieve the position in the list
    final int position = params.getViewAdapterPosition();

    // and add a separator to any view but the last one
    if (position < state.getItemCount()) {
        outRect.set(0, 0, 0, (int) mPaint.getStrokeWidth()); // left, top, right, bottom
    } else {
        outRect.setEmpty(); // 0, 0, 0, 0
    }
}

 

I got some ideas from David Medenjak blog post