gpt4 book ai didi

java - 在元素顶部添加 DividerItemDecoration

转载 作者:太空狗 更新时间:2023-10-29 13:04:03 25 4
gpt4 key购买 nike

我正在尝试在 RecyclerView 中的元素顶部添加分隔线。

根据google我成功添加了一个DividerItemDecoration,代码如下:

DividerItemDecoration:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;

public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}

public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}

public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}

结果:

DividerItemDecoration

我想要完成的事情:

DividerItemDecorationResult

我尝试使用偏移量和索引,但没有得到预期的结果。

如何在我的元素之上添加相同的项目分隔符?

此外,如果有必要,我会留下我的行布局,这是代码:

行:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>

<TextView
android:id="@+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>

<TextView
android:id="@+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>

</RelativeLayout>

最佳答案

说明:在这个答案中,我在 RecyclerView 中的每个项目上方和 RecyclerView 本身之后添加了一个分隔线。这样你就不会出现“过度绘制”,其中 2 条线相互重叠绘制。为此我使用了 View 标签,它从 xml 中绘制了一条简单的线。

行文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<View android:layout_height="2dp"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:background="@color/colorPrimary"/>

<TextView
android:id="@+id/tvDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="italic"
android:textColor="#000"
android:textSize="18sp"/>

<TextView
android:id="@+id/tvBarcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvDescrizione"
android:textColor="#000"
android:textSize="18sp"/>

<TextView
android:id="@+id/tvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvDescrizione"
android:layout_alignParentEnd="true"
android:textColor="#000"
android:textSize="18sp"/>

</RelativeLayout>

主文件:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/repository_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/tmp3"
android:layout_marginTop="30dp"/>

<View
android:id="@+id/bottomLine"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:background="@color/colorPrimary"/>

</LinearLayout>

关于java - 在元素顶部添加 DividerItemDecoration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50795639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com