zoukankan      html  css  js  c++  java
  • RecyclerView 悬浮/粘性头部效果3种方式

    但是以上两种方式onDrawOver()方法实现逻辑对初次查看该段代码要花时间理解。下面代码逻辑(原理一样,同样参考大神代码)相对清晰,易理解

    public class StickyDecoration extends RecyclerView.ItemDecoration {
    ......

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
    View view = parent.getChildAt(i);
    int index = parent.getChildAdapterPosition(view);
    if (mCallback != null) {
    GroupInfo groupinfo = mCallback.getGroupInfo(index);
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();
    //屏幕上第一个可见的 ItemView 时,i == 0;
    if (i != 0) {
    //只有组内的第一个ItemView之上才绘制
    if (groupinfo.isFirstViewInGroup()) {
    int top = view.getTop() - mHeaderHeight;
    int bottom = view.getTop();
    drawHeaderRect(c, groupinfo, left, top, right, bottom);
    }
    } else {
    //当 ItemView 是屏幕上第一个可见的View 时,不管它是不是组内第一个View
    //它都需要绘制它对应的 StickyHeader。
    // 还要判断当前的 ItemView 是不是它组内的最后一个 View
    int top = parent.getPaddingTop();
    if (groupinfo.isLastViewInGroup()) {
    int suggestTop = view.getBottom() - mHeaderHeight;
    // 当 ItemView 与 Header 底部平齐的时候,判断 Header 的顶部是否小于
    // parent 顶部内容开始的位置,如果小于则对 Header.top 进行位置更新,
    //否则将继续保持吸附在 parent 的顶部
    if (suggestTop < top) {
    top = suggestTop;
    }
    }

    int bottom = top + mHeaderHeight;
    drawHeaderRect(c, groupinfo, left, top, right, bottom);
    }
    }
    }
    }

    private void drawHeaderRect(Canvas c, GroupInfo groupinfo, int left, int top, int right, int bottom) {
    //绘制Header
    c.drawRect(left, top, right, bottom, mPaint);
    float titleX = left + mTextOffsetX;
    float titleY = bottom - mFontMetrics.descent;
    //绘制Title
    c.drawText(groupinfo.getTitle(), titleX, titleY, mTextPaint);
    }

    }
    --------------------- 

  • 相关阅读:
    笔试题总结
    ubuntu 14.04 vim install youcompleteme
    c语言位域
    strcmp函数的使用
    Spring多数据源的配置和使用
    根据出生日期计算年龄的sql各种数据库写法
    BZOJ3165 : [Heoi2013]Segment
    BZOJ2725 : [Violet 6]故乡的梦
    BZOJ2851 : 极限满月
    BZOJ2837 : 小强的形状
  • 原文地址:https://www.cnblogs.com/ly570/p/10992447.html
Copyright © 2011-2022 走看看