全是南無阿彌陀佛 發表於 2020-6-2 16:55:00

Android开发 RecyclerView.ItemDecoration

<h1><span style="color: rgba(0, 128, 128, 1)">版权声明</span></h1>
<p>本文来自博客园,作者:观心静&nbsp;,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/13031245.html</p>
<div>本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。</div>
<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1>
<p>  RecyclerView.ItemDecoration是用于实现RecyclerView的Item间距,当然除了实现间距更酷炫的是它可以实现一些在间距上绘制各种分割线。绘制分割线也还是一般操作,深度了解后你甚至可以实现各种时间轴,item分组标题等等功能。因为提供了onDraw方法与Canvas,所以在绘制上自由度极大,可以让你实现各种天马行空的效果。</p>
<h1><span style="color: rgba(0, 128, 128, 1)">主要的三个重写方法</span></h1>
<p><span style="color: rgba(0, 128, 128, 1)">  </span>ItemDecoration只有3个重要的重写方法:</p>
<ul>
<li><strong>getItemOffsets</strong>&nbsp; 用于实现item的上下左右的间距大小</li>
<li><strong>onDraw</strong>&nbsp; 在这个方法里绘制的文字、颜色、图形都会比item更低一层,这些绘制效果如果与item重叠,就会被item遮盖</li>
<li><strong>onDrawOver</strong>&nbsp; 在这个方法绘制的文字、颜色、图形都会比item更高一层,这些绘制效果始终在最上层,不会被遮盖。</li>
</ul>
<p>我们逐一了解这些方法如何使用。</p>
<h1><span style="color: rgba(0, 128, 128, 1)">getItemOffsets</span></h1>
<h3>&nbsp;首先先要写一个RecyclerView列表的Demo来进行演示。这里实现了一个item的背景为蓝色的LinearLayoutManager的列表,如下图:</h3>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602151326329-21263518.png" alt=""></p>
<h3>给列表的item增加上边距</h3>
<p>代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> MainActivity <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> AppCompatActivity {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView mRecyclerView;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerViewAdapter mRecyclerViewAdapter;

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mRecyclerView </span>=<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.recyclerview);
      mRecyclerView.setLayoutManager(</span><span style="color: rgba(0, 0, 255, 1)">new</span> LinearLayoutManager(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">));
      mRecyclerViewAdapter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerViewAdapter();
      mRecyclerView.setAdapter(mRecyclerViewAdapter);
      addData();
      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
      将itemDecoration添加到RecyclerView。
      请注意这里是add的,在底层源码里面可以看到ItemDecoration是可以被添加多个的.这里是一个RecyclerView持有ItemDecoration集合。
      能添加当然就可以移除,所以对应移除的方法
      mRecyclerView.removeItemDecoration();//根据目标移除
      mRecyclerView.removeItemDecorationAt();//根据索引index移除
         </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
      mRecyclerView.addItemDecoration(getItemDecoration());

    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration getItemDecoration() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top </span>= 20;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里增加了20的上边距</span>
<span style="color: rgba(0, 0, 0, 1)">
            }
      };
    }
   
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addData() {
      List</span>&lt;String&gt; list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      list.add(</span>"猎户座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"织女座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"天马座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"天秤座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"剑鱼座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"飞马座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"三角座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"天琴座"<span style="color: rgba(0, 0, 0, 1)">);
      list.add(</span>"蛇夫座"<span style="color: rgba(0, 0, 0, 1)">);
      mRecyclerViewAdapter.refreshData(list);
    }

}</span></pre>
</div>
<p><strong>请注意!</strong>在首次触发的<span style="color: rgba(0, 0, 0, 1)">getItemOffsets返回的View都是没有经过measure测量的,所以这里的View没有尺寸值。但是滚动后重新触发的<span style="color: rgba(0, 0, 0, 1)">getItemOffsets</span>返回的View就有了尺寸值。但是这里返回的<span style="color: rgba(0, 0, 0, 1)">RecyclerView</span>是已经<span style="color: rgba(0, 0, 0, 1)">measure</span>测量过了。</span></p>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602152224286-2097181297.png" alt=""></p>
<p>&nbsp;</p>
<h3>举一反三,我们可以给左边添加边距</h3>
<p>代码:</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration getItemDecoration() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top </span>= 20<span style="color: rgba(0, 0, 0, 1)">;
                outRect.left </span>= 100<span style="color: rgba(0, 0, 0, 1)">;

            }
      };
    }</span></pre>
</div>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602152426396-755761034.png" alt=""></p>
<h3>给指定位置的Item设置边距</h3>
<p>有时候,我们的边距需求并不是全部item都是要求有的,比如我们需求“第一个item有 50 的上边距与最后一个item要求有 50 的下边距”。我们可以根据getItemOffsets 方法提供的 RecyclerView, <span style="color: rgba(0, 0, 0, 1)">RecyclerView.State 这两个值来确定需要实现边距的指定item。</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">代码:</span></p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration getItemDecoration() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration() {
            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (parent.getChildAdapterPosition(view) == 0){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给第一位的item设置50上边距</span>
                  outRect.top = 50<span style="color: rgba(0, 0, 0, 1)">;
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                }
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (parent.getChildAdapterPosition(view) == state.getItemCount() -1){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给最后一位的item设置50下边距</span>
                  outRect.bottom = 50<span style="color: rgba(0, 0, 0, 1)">;<br>              <span style="color: rgba(0, 0, 255, 1)">return<span style="color: rgba(0, 0, 0, 1)">;</span></span>
                }
            }
      };
    }</span></pre>
</div>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602154447579-1012386945.gif" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h1><span style="color: rgba(0, 128, 128, 1)">onDraw</span></h1>
<p>  在重写实现<span style="color: rgba(0, 0, 0, 1)">getItemOffsets</span>方法给item增加边距后,我们可以在onDraw方法实现一些文字,图标等等效果。另外Draw其实是自定义View的知识,如果你还没了解过Android 的Draw是如何实现的,你应该先去了解自定义View。</p>
<h3>给空白边距里绘制文字</h3>
<p>代码:</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration getItemDecoration() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration() {
            </span><span style="color: rgba(0, 0, 255, 1)">private</span> Paint paint = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Paint();

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top </span>= 20<span style="color: rgba(0, 0, 0, 1)">;

            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> count = parent.getChildCount();   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得当前RecyclerView数量</span>
                paint.setColor(Color.RED);            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置画笔为红色</span>
                paint.setTextSize(20);                  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置文字大小</span>
                <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; count; i++) {       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">遍历全部item View</span>
                  View view =<span style="color: rgba(0, 0, 0, 1)"> parent.getChildAt(i);
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> top = view.getTop();            <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得这个item View的top位置</span>
                  <span style="color: rgba(0, 0, 255, 1)">int</span> bottom =<span style="color: rgba(0, 0, 0, 1)"> view.getBottom();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> left =<span style="color: rgba(0, 0, 0, 1)"> view.getLeft();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> right =<span style="color: rgba(0, 0, 0, 1)"> view.getRight();
                  c.drawText(</span>"第" +<span style="color: rgba(0, 0, 0, 1)"> i, left, top, paint);
                }
            }
      };
    }</span></pre>
</div>
<p>这里有些人会有一些误区,认为这里返回的canvas是某一个item下的canvas。(我之前有这样的理解)实际上这里返回的canvas是整个RecyclerView的canvas,如果你把坐标值固定死,也是在RecyclerView里面某个位置绘制这个文字或者图像。所以,这里需要你自己获取全部Item的坐标值,用获取到的Item坐标值来绘制你需要位置上的内容。</p>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602161416149-121451570.png" alt=""></p>
<p>&nbsp;</p>
<h3>给空白边距里绘制分割线</h3>
<p>这里提醒,除了绘制shape分割线,其实还可以使用xml矢量图来绘制图标。</p>
<p>先实现一个shape分割线:</p>
<p>shape_gray_line.xml</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">shape </span><span style="color: rgba(255, 0, 0, 1)">xmlns:android</span><span style="color: rgba(0, 0, 255, 1)">="http://schemas.android.com/apk/res/android"</span><span style="color: rgba(255, 0, 0, 1)"> android:shape</span><span style="color: rgba(0, 0, 255, 1)">="line"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">stroke </span><span style="color: rgba(255, 0, 0, 1)">android:width</span><span style="color: rgba(0, 0, 255, 1)">="5dp"</span><span style="color: rgba(255, 0, 0, 1)"> android:color</span><span style="color: rgba(0, 0, 255, 1)">="@color/yellow_color"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">shape</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>代码:</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Drawable mDividingLineDrawable;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration getItemDecoration() {
      mDividingLineDrawable </span>=<span style="color: rgba(0, 0, 0, 1)"> getDrawable(R.drawable.shape_gray_line);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RecyclerView.ItemDecoration() {

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top </span>= 20<span style="color: rgba(0, 0, 0, 1)">;

            }

            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> count =<span style="color: rgba(0, 0, 0, 1)"> parent.getChildCount();
                parent.getPaddingLeft();
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; count; i++<span style="color: rgba(0, 0, 0, 1)">) {
                  View view </span>=<span style="color: rgba(0, 0, 0, 1)"> parent.getChildAt(i);
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> top =<span style="color: rgba(0, 0, 0, 1)"> view.getTop();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> bottom =<span style="color: rgba(0, 0, 0, 1)"> view.getBottom();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> left =<span style="color: rgba(0, 0, 0, 1)"> view.getLeft();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> right =<span style="color: rgba(0, 0, 0, 1)"> view.getRight();
                  mDividingLineDrawable.setBounds(left , top </span>- 20<span style="color: rgba(0, 0, 0, 1)">, right, top);
                  mDividingLineDrawable.draw(c);
                }
            }
      };
    }</span></pre>
</div>
<p>效果图:</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602165725973-1132478702.png" alt=""></p>
<h2>解释onDraw绘制在Item下层是什么效果</h2>
<p>开头我们说过 “&nbsp;在这个方法里绘制的文字、颜色、图形都会比item更低一层,这些绘制效果如果与item重叠,就会被item遮盖 ” ,要证明这个效果很简单,我们只需要将绘制内容位置与item重叠一下就能明白效果了。</p>
<p>代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> count =<span style="color: rgba(0, 0, 0, 1)"> parent.getChildCount();
                paint.setColor(Color.RED);
                paint.setTextSize(</span>20<span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; count; i++<span style="color: rgba(0, 0, 0, 1)">) {
                  View view </span>=<span style="color: rgba(0, 0, 0, 1)"> parent.getChildAt(i);
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> top =<span style="color: rgba(0, 0, 0, 1)"> view.getTop();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> bottom =<span style="color: rgba(0, 0, 0, 1)"> view.getBottom();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> left =<span style="color: rgba(0, 0, 0, 1)"> view.getLeft();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> right =<span style="color: rgba(0, 0, 0, 1)"> view.getRight();
                  c.drawText(</span>"第" + i, left, top + 10, paint);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里top 增加10 让绘制文字与item重叠</span>
<span style="color: rgba(0, 0, 0, 1)">                }
            }</span></pre>
</div>
<p>效果图:</p>
<p>可以从这个效果图看到,文字被item覆盖了。</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602171850640-868585718.png" alt=""></p>
<p>&nbsp;</p>
<h1><span style="color: rgba(0, 128, 128, 1)">onDrawOver</span></h1>
<p>onDrawOver在使用上与onDraw上是一致的,说这里不在重复说明怎么绘制内容。 这里只解释下onDrawOver的特性与onDraw对比理解。</p>
<h2>解释onDrawOver绘制在Item上层是什么效果</h2>
<p>这里很简单只要在上面onDraw绘制文字的代码了复制一下到onDrawOver里实现就可以了</p>
<p>代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">            @Override
            </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                </span><span style="color: rgba(0, 0, 255, 1)">int</span> count =<span style="color: rgba(0, 0, 0, 1)"> parent.getChildCount();
                paint.setColor(Color.RED);
                paint.setTextSize(</span>20<span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; count; i++<span style="color: rgba(0, 0, 0, 1)">) {
                  View view </span>=<span style="color: rgba(0, 0, 0, 1)"> parent.getChildAt(i);
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> top =<span style="color: rgba(0, 0, 0, 1)"> view.getTop();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> bottom =<span style="color: rgba(0, 0, 0, 1)"> view.getBottom();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> left =<span style="color: rgba(0, 0, 0, 1)"> view.getLeft();
                  </span><span style="color: rgba(0, 0, 255, 1)">int</span> right =<span style="color: rgba(0, 0, 0, 1)"> view.getRight();
                  c.drawText(</span>"第" + i, left, top + 10, paint);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里top 增加10 让绘制文字与item重叠</span>
<span style="color: rgba(0, 0, 0, 1)">                }
            }</span></pre>
</div>
<p>效果图:</p>
<p>可以从这个效果图看到,文字在最上层。</p>
<p><img src="https://img2020.cnblogs.com/blog/1497956/202006/1497956-20200602174137652-1036010294.png" alt=""></p>
<p>&nbsp;</p>
<p>End</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="text-align: center">
    <p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/13031245.html </p>
    <div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/13031245.html
頁: [1]
查看完整版本: Android开发 RecyclerView.ItemDecoration