2 * Copyright © 2021 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/>.
19 // Substantial portions taken from DividerItemDecoration subject to the following license terms:
21 // Copyright 2018 The Android Open Source Project
23 // Licensed under the Apache License, Version 2.0 (the "License");
24 // you may not use this file except in compliance with the License.
25 // You may obtain a copy of the License at
27 // http://www.apache.org/licenses/LICENSE-2.0
29 // Unless required by applicable law or agreed to in writing, software
30 // distributed under the License is distributed on an "AS IS" BASIS,
31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 // See the License for the specific language governing permissions and
33 // limitations under the License.
35 package net.ktnx.mobileledger.ui.templates;
37 import android.content.Context;
38 import android.graphics.Canvas;
39 import android.graphics.Rect;
40 import android.graphics.drawable.Drawable;
41 import android.view.View;
43 import androidx.recyclerview.widget.DividerItemDecoration;
44 import androidx.recyclerview.widget.RecyclerView;
46 import java.util.Objects;
48 class TemplateListDivider extends DividerItemDecoration {
49 private final Rect mBounds = new Rect();
50 private int mOrientation;
51 public TemplateListDivider(Context context, int orientation) {
52 super(context, orientation);
53 mOrientation = orientation;
56 public void setOrientation(int orientation) {
57 super.setOrientation(orientation);
58 mOrientation = orientation;
61 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
62 if (parent.getLayoutManager() == null || getDrawable() == null) {
65 if (mOrientation == VERTICAL) {
66 drawVertical(c, parent);
69 drawHorizontal(c, parent);
73 private void drawVertical(Canvas canvas, RecyclerView parent) {
77 //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
78 if (parent.getClipToPadding()) {
79 left = parent.getPaddingLeft();
80 right = parent.getWidth() - parent.getPaddingRight();
81 canvas.clipRect(left, parent.getPaddingTop(), right,
82 parent.getHeight() - parent.getPaddingBottom());
86 right = parent.getWidth();
89 final Drawable divider = Objects.requireNonNull(getDrawable());
90 final int childCount = parent.getChildCount();
91 final TemplatesRecyclerViewAdapter adapter =
92 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
93 final int itemCount = adapter.getItemCount();
94 for (int i = 0; i < childCount; i++) {
95 final View child = parent.getChildAt(i);
96 final int childAdapterPosition = parent.getChildAdapterPosition(child);
97 if (adapter.getItemViewType(childAdapterPosition) ==
98 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
99 childAdapterPosition + 1 < itemCount &&
100 adapter.getItemViewType(childAdapterPosition + 1) ==
101 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
103 parent.getDecoratedBoundsWithMargins(child, mBounds);
104 final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
105 final int top = bottom - divider.getIntrinsicHeight();
106 divider.setBounds(left, top, right, bottom);
107 divider.draw(canvas);
112 private void drawHorizontal(Canvas canvas, RecyclerView parent) {
116 //noinspection AndroidLintNewApi - NewApi lint fails to handle overrides.
117 if (parent.getClipToPadding()) {
118 top = parent.getPaddingTop();
119 bottom = parent.getHeight() - parent.getPaddingBottom();
120 canvas.clipRect(parent.getPaddingLeft(), top,
121 parent.getWidth() - parent.getPaddingRight(), bottom);
125 bottom = parent.getHeight();
128 final Drawable divider = Objects.requireNonNull(getDrawable());
129 final int childCount = parent.getChildCount();
130 final TemplatesRecyclerViewAdapter adapter =
131 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
132 final int itemCount = adapter.getItemCount();
133 for (int i = 0; i < childCount; i++) {
134 final View child = parent.getChildAt(i);
135 final int childAdapterPosition = parent.getChildAdapterPosition(child);
136 if (adapter.getItemViewType(childAdapterPosition) ==
137 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
138 childAdapterPosition + 1 < itemCount &&
139 adapter.getItemViewType(childAdapterPosition + 1) ==
140 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
142 parent.getLayoutManager()
143 .getDecoratedBoundsWithMargins(child, mBounds);
144 final int right = mBounds.right + Math.round(child.getTranslationX());
145 final int left = right - divider.getIntrinsicWidth();
146 divider.setBounds(left, top, right, bottom);
147 divider.draw(canvas);
152 public void getItemOffsets(Rect outRect, View child, RecyclerView parent,
153 RecyclerView.State state) {
154 final int childAdapterPosition = parent.getChildAdapterPosition(child);
155 final TemplatesRecyclerViewAdapter adapter =
156 (TemplatesRecyclerViewAdapter) Objects.requireNonNull(parent.getAdapter());
157 final int itemCount = adapter.getItemCount();
159 if (adapter.getItemViewType(childAdapterPosition) ==
160 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER ||
161 childAdapterPosition + 1 < itemCount &&
162 adapter.getItemViewType(childAdapterPosition + 1) ==
163 TemplatesRecyclerViewAdapter.ITEM_TYPE_DIVIDER)
166 super.getItemOffsets(outRect, child, parent, state);