一、库

做成了库,可以直接引入。可以实现的功能是,针对 RecyclerView 设置悬浮标题,并且同时可以设置分割线。地址

下面是引入方法:
To get a Git project into your build:
Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://www.jitpack.io' }
  5. }
  6. }

Step 2. Add the dependency

  1. dependencies {
  2. implementation 'com.gitee.quetzalcoatl:sticky-decration:1.0.4'
  3. }

Share this release:
Tweet 最新版:
悬浮标题(带仓库地址) - 图1
**

二、使用详解

数据结构比较简单,只要有一个属性用来标识悬浮内容即可,下面是一个简单的类。

  1. class CategoryBean(val tag: String, val categoryName: String): Serializable
    1. 线行布局

iShot2020-10-25 14.27.51.gif

  1. val itemDecoration = StickyDecoration.Builder.init {
  2. if (lists.size > it && it > - 1){
  3. lists[it].tag
  4. }else ""
  5. }.setGroupBackground(Color.parseColor("#07C0C2"))
  6. //设置悬浮标题整体背景,颜色可以传资源文件StickyUtils.getColor(this,R.color.colorAccent)
  7. .setGroupHeight(StickyUtils.dp2px(this,40))//设置悬浮标题高度
  8. .setGroupTextColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))//设置标题文字颜色
  9. .setGroupTextSize(StickyUtils.dp2px(this,20))//设置标题文字大小,可以传资源文件size
  10. .setTextSideMargin(StickyUtils.dp2px(this,20))//设置标题的边距
  11. //设置分割线相关
  12. .setDivideColor(StickyUtils.getColor(this,R.color.colorPrimary))//分割线颜色
  13. .setDivideHeight(StickyUtils.dp2px(this,2))//分割线高度
  14. .setDivideLeftPadding(StickyUtils.dp2px(this,20))//分割线左侧间距
  15. .setOnClickListener { position, id ->
  16. //悬浮标题点击监听
  17. Toast.makeText(this, lists[position].tag, Toast.LENGTH_SHORT).show()
  18. }
  19. .build()

上面可以设置悬浮内容,高度和分割线等,设置好以后调用 addItemDecration 就可以了,如下:

  1. rv_list?.run {
  2. layoutManager = LinearLayoutManager(this@LinearActivity)
  3. adapter = mAdapter
  4. addItemDecoration(itemDecoration)
  5. }
  6. mAdapter.setList(lists)
  • 2.线性布局自定义悬浮

iShot2020-10-25 14.29.03.gif

  1. val itemDecoration = PowerfulStickyDecoration.Builder.init(object : PowerGroupListener{
  2. override fun getGroupName(position: Int): String {
  3. return if (lists.size > position && position > -1){
  4. lists[position].tag
  5. }else{
  6. ""
  7. }
  8. }
  9. override fun getGroupView(position: Int): View {
  10. val customView = layoutInflater.inflate(R.layout.item_sticky_custom_top,null,false)
  11. //在这里获取悬浮布局,并设置数据等
  12. if (lists.size > position && position > -1){
  13. customView.tv_item_sticky_custom_top_title.text = lists[position].tag
  14. }
  15. return customView
  16. }
  17. }).setDivideColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))
  18. .setDivideHeight(StickyUtils.dp2px(this, 1f))
  19. .setOnClickListener { position, id ->
  20. val datas = mAdapter.data
  21. if (datas.size > position) {
  22. Toast.makeText(this, "--${datas[position].tag}", Toast.LENGTH_SHORT).show()
  23. }
  24. }.build()
  25. rv_list?.run {
  26. layoutManager = LinearLayoutManager(this@LinearCustomActivity)
  27. adapter = mAdapter
  28. addItemDecoration(itemDecoration)
  29. }
  30. mAdapter.setList(lists)
  • 3.网格布局

iShot2020-10-25 14.33.19.gif

  1. val gridLayoutManager = GridLayoutManager(this,2)
  2. val size = resources.getDimensionPixelOffset(R.dimen.sp_14)
  3. val itemDecoration = StickyDecoration.Builder.init {
  4. if (lists.size > it && it > - 1){
  5. lists[it].tag
  6. }else ""
  7. }.setGroupBackground(Color.parseColor("#07C0C2"))
  8. //设置悬浮标题整体背景,颜色可以传资源文件StickyUtils.getColor(this,R.color.colorAccent)
  9. .setGroupHeight(StickyUtils.dp2px(this,40))//设置悬浮标题高度
  10. .setGroupTextColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))//设置标题文字颜色
  11. .setGroupTextSize(StickyUtils.dp2px(this,20))//设置标题文字大小,可以传资源文件size
  12. .setTextSideMargin(StickyUtils.dp2px(this,20))//设置标题的边距
  13. //设置分割线相关
  14. .setDivideColor(StickyUtils.getColor(this,R.color.colorPrimary))//分割线颜色
  15. .setDivideHeight(StickyUtils.dp2px(this,2))//分割线高度
  16. .setDivideLeftPadding(StickyUtils.dp2px(this,20))//分割线左侧间距
  17. .resetSpan(rv_list,gridLayoutManager)
  18. .build()
  19. rv_list?.run {
  20. layoutManager = gridLayoutManager
  21. adapter = mAdapter
  22. addItemDecoration(itemDecoration)
  23. }
  24. mAdapter.setList(lists)
  • 4.网格布局自定义悬浮 ``` val lists = DataUtil.getCategoryList()

    1. //lists.addAll(lists)
    2. val gridLayoutManager = GridLayoutManager(this, 2)
    3. val size = resources.getDimensionPixelOffset(R.dimen.sp_14)
    4. val itemDecoration = PowerfulStickyDecoration.Builder.init(object : PowerGroupListener {
    5. override fun getGroupName(position: Int): String {
    6. return if (lists.size > position && position > -1){
    7. lists[position].tag
    8. }else{
    9. ""
    10. }
    11. }
    12. //设置自定义悬浮布局
    13. override fun getGroupView(position: Int): View {
    14. val customView = layoutInflater.inflate(R.layout.item_sticky_custom_top,null,false)
    15. //在这里获取悬浮布局,并设置数据等
    16. if (lists.size > position && position > -1){
    17. customView.tv_item_sticky_custom_top_title.text = lists[position].tag
    18. }
    19. return customView
    20. }
    21. })
    22. //设置分割线
    23. .setDivideColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))
    24. .setDivideHeight(StickyUtils.dp2px(this, 1f))
    25. .setOnClickListener { position, id ->
    26. val datas = mAdapter.data
    27. if (datas.size > position) {
    28. Toast.makeText(this, "--${datas[position].tag}", Toast.LENGTH_SHORT).show()
    29. }
    30. }.resetSpan(rv_grid_custom_lsit,gridLayoutManager)
    31. .build()

// itemDecoration.resetSpan(rv_grid_custom_lsit, gridLayoutManager) rv_grid_custom_lsit?.run { layoutManager = gridLayoutManager adapter = mAdapter addItemDecoration(itemDecoration) } mAdapter.setList(lists) ```

三、地址

demo