RecyclerView:
- Show Scrollbar on RecyclerView https://stackoverflow.com/questions/3103132/android-listview-scrollbarstyle
- Show DividerItemDecoration between items in RecyclerView
RecyclerView:
| 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); | |
| } | |
| } | |
| } |