Android recyclerview realizes the effects of suspended ceiling, separation line and bottom prompt

All the effects in this article are realized through itemdecoration, which can be decoupled from the business, making the recyclerview template more concise and not caring about any auxiliary UI and GitHub address

1、 Top adsorption effect drawing

2、 Effect drawing of top non adsorption

3、 Less than one screen effect

4、 Core implementation point

1. Why can it be realized through itemdecoration?

① Get the left, top, right and bottom margins of the current template view through the getitemoffsets () method. These margins are used to draw these auxiliary UIs.

// RecyclerView的measure child方法
public void measureChild(@NonNull View child,int widthUsed,int heightUsed) {
      final LayoutParams lp = (LayoutParams) child.getLayoutParams();
  //将getItemOffsets()获取的值累加到测量值之中
      final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child);
      widthUsed += insets.left + insets.right;
      heightUsed += insets.top + insets.bottom;
      final int widthSpec = getChildMeasureSpec(getWidth(),getWidthMode(),getPaddingLeft() + getPaddingRight() + widthUsed,lp.width,canScrollHorizontally());
      final int heightSpec = getChildMeasureSpec(getHeight(),getHeightMode(),getPaddingTop() + getPaddingBottom() + heightUsed,lp.height,canScrollVertically());
      if (shouldMeasureChild(child,widthSpec,heightSpec,lp)) {
        child.measure(widthSpec,heightSpec);
      }
    }

② Draw the floating view through ondrawover(), and the drawn UI is above all sub views.

@Override
  public void draw(Canvas c) {
    super.draw(c);
 //在RecyclerView绘制完之后回调onDrawOver()方法
    final int count = mItemDecorations.size();
    for (int i = 0; i < count; i++) {
      mItemDecorations.get(i).onDrawOver(c,this,mState);
    }
 }

③ Draw views such as split lines through the OnDraw () method.

 public void onDraw(Canvas c) {
    super.onDraw(c);
 //先回调onDraw()方法,在绘制RecyclerView子view
    final int count = mItemDecorations.size();
    for (int i = 0; i < count; i++) {
      mItemDecorations.get(i).onDraw(c,mState);
    }
  }

2. Drawing of "prompt to the end"

Since the width and height of the sub view cannot be obtained in getitemoffsets(), there is no measure at this time. After adding the height in getitemoffsets(), if it is less than one screen, it needs to be corrected in the ondraw() method. The correction method is to modify the mdecorinsets property by reflection and reset the value set in the getitemoffsets() method.

private void setDecorInsetsBottom(RecyclerView.LayoutParams param,int bottom) {
    try {
      // 找到RecyclerView.LayoutParams中的mDecorInsets属性值
      Field filed = RecyclerView.LayoutParams.class.getDeclaredField("mDecorInsets");
      filed.setAccessible(true);
      Rect decorRect = (Rect) filed.get(param);
      decorRect.bottom = bottom;
    } catch (Exception e) {
    }
  }

summary

The above is what Xiaobian introduced to you. Android recyclerview realizes the effect of suspended ceiling, separation line and prompt in the end. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>