Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Created November 13, 2019 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kolyall/f30874d1e5e657651d78d3681cca231a to your computer and use it in GitHub Desktop.
Save Kolyall/f30874d1e5e657651d78d3681cca231a to your computer and use it in GitHub Desktop.
RecyclerView patterns
android:scrollbarSize="2dp"
android:scrollbarStyle="insideOverlay"
android:fadeScrollbars="false"
android:scrollbarThumbVertical="@drawable/app_scrollbar_recycler_view"//with margin
android:scrollbars="vertical"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="4dp"> <!-- Your Margin -->
<shape>
<solid android:color="@color/colorAccent" /> <!-- Your Color -->
<corners android:radius="2dp" /> <!-- Your Radius -->
<size android:width="4dp" /> <!-- Your Width -->
</shape>
</item>
</layer-list>
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
/**
* Created by Nikolay Unuchek on 08.02.2017.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
/**
* Default divider will be used
*/
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
mDivider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
/**
* Custom divider will be used
*/
public DividerItemDecoration(Context context, @DrawableRes int resId) {
mDivider = ContextCompat.getDrawable(context, resId);
}
@Override
public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment