我试图在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);
}
}
}
结果:
我想要完成的任务:
我试图使用偏移和索引,但没有得到预期的结果。
如何在元素上添加相同的项目分隔符?
如果有必要,我会留下我的行布局,这里是代码:
行:
<?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>
答案 0 :(得分:2)
说明:在这个答案中,我在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>
答案 1 :(得分:1)
不要通过项目装饰添加分隔符。而是在行代码的上方和下方放置两个视图,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
<RelativeLayout
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>
<View android:layout_height="1px"
android:layout_width="match_parent"
android:background="#000000"
/>
</LinearLayout>