• Android recyclerview 浮动头部


    1. package com.tal.jxtteacher.uijxt.weight;
    2. import android.content.Context;
    3. import android.graphics.Canvas;
    4. import android.graphics.Color;
    5. import android.graphics.Paint;
    6. import android.graphics.Rect;
    7. import android.support.v7.widget.LinearLayoutManager;
    8. import android.support.v7.widget.RecyclerView;
    9. import android.support.v7.widget.RecyclerView.ItemDecoration;
    10. import android.view.View;
    11. import com.tal.jxtteacher.uijxt.adapter.RecyclerViewAdapter;
    12. public class CreatGroupPickContactsItemDecoration extends ItemDecoration {
    13. //头部的高
    14. private int mItemHeaderHeight;
    15. private int mTextPaddingLeft;
    16. //画笔,绘制头部和分割线
    17. private Paint mItemHeaderPaint;
    18. private Paint mTextPaint;
    19. private Paint mLinePaint;
    20. private Rect mTextRect;
    21. private Context context;
    22. public CreatGroupPickContactsItemDecoration(Context context) {
    23. this.context = context;
    24. mItemHeaderHeight = dp2px(context, 40);
    25. mTextPaddingLeft = dp2px(context, 0);
    26. mTextRect = new Rect();
    27. mItemHeaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    28. mItemHeaderPaint.setColor(Color.WHITE);
    29. mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    30. mTextPaint.setTextSize(46);
    31. mTextPaint.setColor(Color.parseColor("#979797"));
    32. mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    33. mLinePaint.setStyle(Paint.Style.FILL);
    34. mLinePaint.setColor(Color.parseColor("#EFEFEF"));
    35. }
    36. /**
    37. * 绘制Item的分割线和组头
    38. *
    39. * @param c
    40. * @param parent
    41. * @param state
    42. */
    43. @Override
    44. public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    45. if (parent.getAdapter() instanceof RecyclerViewAdapter) {
    46. RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
    47. int count = parent.getChildCount();//获取可见范围内Item的总数
    48. for (int i = 0; i < count; i++) {
    49. View view = parent.getChildAt(i);
    50. int position = parent.getChildLayoutPosition(view);
    51. boolean isHeader = adapter.isItemHeader(position);
    52. int left = parent.getPaddingLeft();
    53. int right = parent.getWidth() - parent.getPaddingRight();
    54. if (isHeader) {
    55. c.drawRect(left, view.getTop() - mItemHeaderHeight, right, view.getTop(), mItemHeaderPaint);
    56. mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
    57. c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, (view.getTop() - mItemHeaderHeight) + mItemHeaderHeight / 2 + mTextRect.height() / 2, mTextPaint);
    58. c.drawRect(left,view.getTop()-mItemHeaderHeight,right,view.getTop()-mItemHeaderHeight+dp2px(context,1),mLinePaint);
    59. c.drawRect(left,view.getTop() - dp2px(context,1),right,view.getTop(),mLinePaint);
    60. } else {
    61. c.drawRect(left, view.getTop() - dp2px(context,1), right, view.getTop(), mLinePaint);
    62. }
    63. }
    64. }
    65. }
    66. /**
    67. * 绘制Item的顶部布局(吸顶效果)
    68. *
    69. * @param c
    70. * @param parent
    71. * @param state
    72. */
    73. @Override
    74. public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    75. if (parent.getAdapter() instanceof RecyclerViewAdapter) {
    76. RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
    77. int position = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition();
    78. if (position < 0){
    79. return;
    80. }
    81. View view = parent.findViewHolderForAdapterPosition(position).itemView;
    82. boolean isHeader = adapter.isItemHeader(position + 1);
    83. int top = parent.getPaddingTop();
    84. int left = parent.getPaddingLeft();
    85. int right = parent.getWidth() - parent.getPaddingRight();
    86. if (isHeader) {
    87. int bottom = Math.min(mItemHeaderHeight, view.getBottom());
    88. c.drawRect(left, top + view.getTop() - mItemHeaderHeight, right, top + bottom, mItemHeaderPaint);
    89. mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
    90. c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, top + mItemHeaderHeight / 2 + mTextRect.height() / 2 - (mItemHeaderHeight - bottom), mTextPaint);
    91. } else {
    92. c.drawRect(left, top, right, top + mItemHeaderHeight, mItemHeaderPaint);
    93. mTextPaint.getTextBounds(adapter.getGroupName(position), 0, adapter.getGroupName(position).length(), mTextRect);
    94. c.drawText(adapter.getGroupName(position), left + mTextPaddingLeft, top + mItemHeaderHeight / 2 + mTextRect.height() / 2, mTextPaint);
    95. }
    96. c.save();
    97. }
    98. }
    99. /**
    100. * 设置Item的间距
    101. *
    102. * @param outRect
    103. * @param view
    104. * @param parent
    105. * @param state
    106. */
    107. @Override
    108. public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    109. if (parent.getAdapter() instanceof RecyclerViewAdapter) {
    110. RecyclerViewAdapter adapter = (RecyclerViewAdapter) parent.getAdapter();
    111. int position = parent.getChildLayoutPosition(view);
    112. boolean isHeader = adapter.isItemHeader(position);
    113. if (isHeader) {
    114. outRect.top = mItemHeaderHeight;
    115. } else {
    116. outRect.top = dp2px(context,1);
    117. }
    118. }
    119. }
    120. /**
    121. * dp转换成px
    122. */
    123. private int dp2px(Context context, float dpValue) {
    124. float scale = context.getResources().getDisplayMetrics().density;
    125. return (int) (dpValue * scale + 0.5f);
    126. }
    127. }

  • 相关阅读:
    【报错】QT Release NO CMAKE_CXX_COMPILER could be found
    大学生个人网站作业 超简单DIV CSS个人网页成品 简单个人网站作业模板 HTML个人网页设计下载 简约黑白色个人主页
    博客系统自动化测试
    《Linux》day2--tmux和vim
    【C++进阶】多态(万字详解)
    【C语言航路】第七站:结构体初阶
    Kafka从入门到精通
    Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成
    边缘计算物联网网关在机械加工行业中的效用分享
    1000元订金?华为折叠屏手机MateX5今日开始预订,售价尚未公布
  • 原文地址:https://blog.csdn.net/qq_26331411/article/details/118754130