Advertisement
danikula

Single RecyclerView divider

Jul 19th, 2018
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public class SingleDividerDecoration extends DividerItemDecoration {
  2.  
  3.     private final int height;
  4.     private int position = -1;
  5.  
  6.     public SingleDividerDecoration(Context context, @DrawableRes int dividerId) {
  7.         super(context, LinearLayout.VERTICAL);
  8.         Drawable divider = ContextCompat.getDrawable(context, dividerId);
  9.         this.height = divider.getIntrinsicHeight();
  10.         setDrawable(divider);
  11.     }
  12.  
  13.     public void setPosition(int position) {
  14.         this.position = position;
  15.     }
  16.  
  17.     @Override
  18.     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  19.         int viewPosition = parent.getChildAdapterPosition(view);
  20.         int dividerHeight = viewPosition == position ? height : 0;
  21.         outRect.set(0, 0, 0, dividerHeight);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement