简体   繁体   English

在RecyclerView的开头添加DividerItemDecoration效果

[英]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.

相关问题 在RecyclerView的两侧添加DividerItemDecoration - Add DividerItemDecoration both sides of recyclerView 无法为 RecyclerView 的 DividerItemDecoration 设置自定义 Drawable - Unable to set Custom Drawable for DividerItemDecoration for RecyclerView DividerItemDecoration没有出现在RecyclerView中 - DividerItemDecoration doesn't show up in RecyclerView 使用DividerItemDecoration在recyclerview中隐藏一些项目分隔线 - Hide some item dividers in recyclerview using DividerItemDecoration 如何为 DividerItemDecoration Android 添加边距开始 - How to add margin start to DividerItemDecoration Android 为什么 DividerItemDecoration 在 recyclerView 片段中获得空对象引用? - Why DividerItemDecoration get a null object reference in recyclerView fragment? 调用 RecyclerView.Adapter.notifyItemInserted() 时不应用 DividerItemDecoration - DividerItemDecoration is not applied when RecyclerView.Adapter.notifyItemInserted() is called RecyclerView:使用自定义 ItemDecoration 为最后一个项目添加底部间距不适用于 DividerItemDecoration 和 ItemTouchHelper - RecyclerView: Adding bottom spacing for last item with a custom ItemDecoration doesn't work well with DividerItemDecoration and ItemTouchHelper 如何在ListView的开头添加一个特殊项目 - How to add an special item on the begining of the ListView RecyclerView 中没有涟漪效应 - No Ripple Effect in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM