122 lines
4.7 KiB
Java
122 lines
4.7 KiB
Java
package androidx.recyclerview.widget;
|
|
|
|
import android.content.Context;
|
|
import android.content.res.TypedArray;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Rect;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
|
|
private static final int[] ATTRS = {16843284};
|
|
public static final int HORIZONTAL = 0;
|
|
private static final String TAG = "DividerItem";
|
|
public static final int VERTICAL = 1;
|
|
private final Rect mBounds = new Rect();
|
|
private Drawable mDivider;
|
|
private int mOrientation;
|
|
|
|
public DividerItemDecoration(Context context, int i) {
|
|
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(ATTRS);
|
|
Drawable drawable = obtainStyledAttributes.getDrawable(0);
|
|
this.mDivider = drawable;
|
|
if (drawable == null) {
|
|
Log.w("DividerItem", "@android:attr/listDivider was not set in the theme used for this DividerItemDecoration. Please set that attribute all call setDrawable()");
|
|
}
|
|
obtainStyledAttributes.recycle();
|
|
setOrientation(i);
|
|
}
|
|
|
|
private void drawHorizontal(Canvas canvas, RecyclerView recyclerView) {
|
|
int i;
|
|
int i2;
|
|
canvas.save();
|
|
if (recyclerView.getClipToPadding()) {
|
|
i2 = recyclerView.getPaddingTop();
|
|
i = recyclerView.getHeight() - recyclerView.getPaddingBottom();
|
|
canvas.clipRect(recyclerView.getPaddingLeft(), i2, recyclerView.getWidth() - recyclerView.getPaddingRight(), i);
|
|
} else {
|
|
i = recyclerView.getHeight();
|
|
i2 = 0;
|
|
}
|
|
int childCount = recyclerView.getChildCount();
|
|
for (int i3 = 0; i3 < childCount; i3++) {
|
|
View childAt = recyclerView.getChildAt(i3);
|
|
recyclerView.getLayoutManager().getDecoratedBoundsWithMargins(childAt, this.mBounds);
|
|
int round = Math.round(childAt.getTranslationX()) + this.mBounds.right;
|
|
this.mDivider.setBounds(round - this.mDivider.getIntrinsicWidth(), i2, round, i);
|
|
this.mDivider.draw(canvas);
|
|
}
|
|
canvas.restore();
|
|
}
|
|
|
|
private void drawVertical(Canvas canvas, RecyclerView recyclerView) {
|
|
int i;
|
|
int i2;
|
|
canvas.save();
|
|
if (recyclerView.getClipToPadding()) {
|
|
i2 = recyclerView.getPaddingLeft();
|
|
i = recyclerView.getWidth() - recyclerView.getPaddingRight();
|
|
canvas.clipRect(i2, recyclerView.getPaddingTop(), i, recyclerView.getHeight() - recyclerView.getPaddingBottom());
|
|
} else {
|
|
i = recyclerView.getWidth();
|
|
i2 = 0;
|
|
}
|
|
int childCount = recyclerView.getChildCount();
|
|
for (int i3 = 0; i3 < childCount; i3++) {
|
|
View childAt = recyclerView.getChildAt(i3);
|
|
recyclerView.getDecoratedBoundsWithMargins(childAt, this.mBounds);
|
|
int round = Math.round(childAt.getTranslationY()) + this.mBounds.bottom;
|
|
this.mDivider.setBounds(i2, round - this.mDivider.getIntrinsicHeight(), i, round);
|
|
this.mDivider.draw(canvas);
|
|
}
|
|
canvas.restore();
|
|
}
|
|
|
|
@Nullable
|
|
public Drawable getDrawable() {
|
|
return this.mDivider;
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.ItemDecoration
|
|
public void getItemOffsets(Rect rect, View view, RecyclerView recyclerView, RecyclerView.State state) {
|
|
Drawable drawable = this.mDivider;
|
|
if (drawable == null) {
|
|
rect.set(0, 0, 0, 0);
|
|
} else if (this.mOrientation == 1) {
|
|
rect.set(0, 0, 0, drawable.getIntrinsicHeight());
|
|
} else {
|
|
rect.set(0, 0, drawable.getIntrinsicWidth(), 0);
|
|
}
|
|
}
|
|
|
|
@Override // androidx.recyclerview.widget.RecyclerView.ItemDecoration
|
|
public void onDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.State state) {
|
|
if (recyclerView.getLayoutManager() != null && this.mDivider != null) {
|
|
if (this.mOrientation == 1) {
|
|
drawVertical(canvas, recyclerView);
|
|
} else {
|
|
drawHorizontal(canvas, recyclerView);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setDrawable(@NonNull Drawable drawable) {
|
|
if (drawable != null) {
|
|
this.mDivider = drawable;
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("Drawable cannot be null.");
|
|
}
|
|
|
|
public void setOrientation(int i) {
|
|
if (i == 0 || i == 1) {
|
|
this.mOrientation = i;
|
|
return;
|
|
}
|
|
throw new IllegalArgumentException("Invalid orientation. It should be either HORIZONTAL or VERTICAL");
|
|
}
|
|
}
|