2 * Copyright © 2022 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.templates;
20 import android.content.Context;
21 import android.graphics.Canvas;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.view.View;
26 import androidx.recyclerview.widget.DividerItemDecoration;
27 import androidx.recyclerview.widget.RecyclerView;
29 import java.util.Objects;
31 class TemplateListDivider extends DividerItemDecoration {
32 private final Rect mBounds = new Rect();
33 private int mOrientation;
34 public TemplateListDivider(Context context, int orientation) {
35 super(context, orientation);
36 mOrientation = orientation;
39 public void setOrientation(int orientation) {
40 super.setOrientation(orientation);
41 mOrientation = orientation;
44 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
45 if (parent.getLayoutManager() == null || getDrawable() == null) {
48 if (mOrientation == VERTICAL) {
49 drawVertical(c, parent);
52 drawHorizontal(c, parent);
56 private void drawVertical(Canvas canvas, RecyclerView parent) {
60 //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
61 if (parent.getClipToPadding()) {
62 left = parent.getPaddingLeft();
63 right = parent.getWidth() - parent.getPaddingRight();
64 canvas.clipRect(left, parent.getPaddingTop(), right,
65 parent.getHeight() - parent.getPaddingBottom());
69 right = parent.getWidth();
72 final Drawable divider = Objects.requireNonNull(getDrawable());
73 final int childCount = parent.getChildCount();
74 final TemplatesRecyclerViewAdapter adapter =
75 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
76 final int itemCount = adapter.getItemCount();
77 for (int i = 0; i < childCount; i++) {
78 final View child = parent.getChildAt(i);
79 final int childAdapterPosition = parent.getChildAdapterPosition(child);
80 if (childAdapterPosition == RecyclerView.NO_POSITION ||
81 adapter.getItemViewType(childAdapterPosition) ==
82 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
83 childAdapterPosition + 1 < itemCount &&
84 adapter.getItemViewType(childAdapterPosition + 1) ==
85 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
87 parent.getDecoratedBoundsWithMargins(child, mBounds);
88 final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
89 final int top = bottom - divider.getIntrinsicHeight();
90 divider.setBounds(left, top, right, bottom);
96 private void drawHorizontal(Canvas canvas, RecyclerView parent) {
97 final RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
98 if (layoutManager == null)
104 //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
105 if (parent.getClipToPadding()) {
106 top = parent.getPaddingTop();
107 bottom = parent.getHeight() - parent.getPaddingBottom();
108 canvas.clipRect(parent.getPaddingLeft(), top,
109 parent.getWidth() - parent.getPaddingRight(), bottom);
113 bottom = parent.getHeight();
116 final Drawable divider = Objects.requireNonNull(getDrawable());
117 final int childCount = parent.getChildCount();
118 final TemplatesRecyclerViewAdapter adapter =
119 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
120 final int itemCount = adapter.getItemCount();
121 for (int i = 0; i < childCount; i++) {
122 final View child = parent.getChildAt(i);
123 final int childAdapterPosition = parent.getChildAdapterPosition(child);
124 if (childAdapterPosition == RecyclerView.NO_POSITION ||
125 adapter.getItemViewType(childAdapterPosition) ==
126 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
127 childAdapterPosition + 1 < itemCount &&
128 adapter.getItemViewType(childAdapterPosition + 1) ==
129 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
133 layoutManager.getDecoratedBoundsWithMargins(child, mBounds);
134 final int right = mBounds.right + Math.round(child.getTranslationX());
135 final int left = right - divider.getIntrinsicWidth();
136 divider.setBounds(left, top, right, bottom);
137 divider.draw(canvas);
142 public void getItemOffsets(Rect outRect, View child, RecyclerView parent,
143 RecyclerView.State state) {
144 final int childAdapterPosition = parent.getChildAdapterPosition(child);
145 final TemplatesRecyclerViewAdapter adapter =
146 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
147 final int itemCount = adapter.getItemCount();
149 if (childAdapterPosition == RecyclerView.NO_POSITION ||
150 adapter.getItemViewType(childAdapterPosition) ==
151 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
152 childAdapterPosition + 1 < itemCount &&
153 adapter.getItemViewType(childAdapterPosition + 1) ==
154 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
157 super.getItemOffsets(outRect, child, parent, state);