[英]Add DividerItemDecoration effect in the begining of RecyclerView
I have a layout that consists of a TextView with a helper text on the top of the layout followed by a RecyclerView with the related items. 我有一个布局,该布局包含一个TextView和一个带有相关项目的RecyclerView,该TextView在布局的顶部带有辅助文本。
I added a DividerItemDecoration to the RecyclerView, but it only divides (I know, that's its name) the elements, but not the helper TextView from the first element of the RecyclerView. 我在RecyclerView中添加了DividerItemDecoration,但它仅对元素进行划分(我知道,这就是它的名字),但不对RecyclerView的第一个元素进行辅助TextView。
Is there a way to extend the DividerItemDecoration in any way or I need to place an empty View element with background between the TextView and RecyclerView? 有没有办法以任何方式扩展DividerItemDecoration,或者我需要在TextView和RecyclerView之间放置一个带有背景的空View元素?
There is no way to do what you want using the build-in DividerItemDecoration
class. 使用内置的DividerItemDecoration
类无法执行DividerItemDecoration
。
The divider is drawn in a two-step process. 分压器是通过两步过程绘制的。 First, getItemOffsets()
is used to add space to the bottom of each item in the RecyclerView
. 首先,使用getItemOffsets()
在RecyclerView
的每个项目的底部添加空间。 Then, onDraw()
is used to draw the divider within that space. 然后,使用onDraw()
在该空间内绘制分隔线。
A look at the source code: 看一下源代码:
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
...
if (mOrientation == VERTICAL) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
Here you can see that space is only added to the bottom (or right, in horizontal mode) of each item. 在这里您可以看到空间仅添加到每个项目的底部 (或在水平模式下为右侧)。 There's no special case for the first item to give it a top offset as well. 对于第一个项目也没有特殊情况,也要给它一个最高偏移量。
private void drawVertical(Canvas canvas, RecyclerView parent) {
...
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, mBounds);
final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
final int top = bottom - mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(canvas);
}
...
}
Here you can see that the vertical boundaries for the mDivider
drawable are computed based on bottom edge of each child view. 在这里,您可以看到mDivider
可绘制对象的垂直边界是根据每个子视图的底部边缘计算的。
You could, of course, create your own implementation of RecyclerView.ItemDecoration
, and create a special case for the first item in the list. 当然,您可以创建自己的RecyclerView.ItemDecoration
实现,并为列表中的第一项创建特殊情况。
Edit : Here's a stripped-down and simplified ItemDecoration
that is based on DividerItemDecoration
but also draws a divider on top of the first item: https://gist.github.com/zizibaloob/0c6be3e1318257950507e9c614c8aa70 编辑 :这是一个基于DividerItemDecoration
简化精简ItemDecoration
,但还在第一个项目的顶部绘制了分隔线: https : DividerItemDecoration
You can use View in your xml to divide recyclerView and TextView as below : 您可以在XML中使用View来划分recyclerView和TextView,如下所示:
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.