ConversationLineItemDecoration.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright 2017 Andrew Dawson
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky;
  16. import android.content.Context;
  17. import android.content.res.Resources;
  18. import android.graphics.Canvas;
  19. import android.graphics.drawable.Drawable;
  20. import android.support.v7.widget.RecyclerView;
  21. import android.util.TypedValue;
  22. import android.view.View;
  23. import static android.util.TypedValue.COMPLEX_UNIT_DIP;
  24. class ConversationLineItemDecoration extends RecyclerView.ItemDecoration {
  25. private final Context mContext;
  26. private final Drawable mDivider;
  27. public ConversationLineItemDecoration(Context context, Drawable divider) {
  28. mContext = context;
  29. mDivider = divider;
  30. }
  31. @Override
  32. public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
  33. int dividerLeft = parent.getPaddingLeft() + mContext.getResources().getDimensionPixelSize(R.dimen.status_left_line_margin);
  34. int dividerRight = dividerLeft + mDivider.getIntrinsicWidth();
  35. int childCount = parent.getChildCount();
  36. int avatarMargin = mContext.getResources().getDimensionPixelSize(R.dimen.account_avatar_margin);
  37. for (int i = 0; i < childCount; i++) {
  38. View child = parent.getChildAt(i);
  39. int dividerTop = child.getTop() + (i == 0 ? avatarMargin : 0);
  40. int dividerBottom = (i == childCount - 1 ? child.getTop() + avatarMargin : child.getBottom());
  41. mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
  42. mDivider.draw(c);
  43. }
  44. }
  45. }