一、库
做成了库,可以直接引入。可以实现的功能是,针对 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:
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.gitee.quetzalcoatl:sticky-decration:1.0.4'
}
Share this release:
Tweet 最新版:
**
二、使用详解
数据结构比较简单,只要有一个属性用来标识悬浮内容即可,下面是一个简单的类。
class CategoryBean(val tag: String, val categoryName: String): Serializable
- 线行布局
val itemDecoration = StickyDecoration.Builder.init {
if (lists.size > it && it > - 1){
lists[it].tag
}else ""
}.setGroupBackground(Color.parseColor("#07C0C2"))
//设置悬浮标题整体背景,颜色可以传资源文件StickyUtils.getColor(this,R.color.colorAccent)
.setGroupHeight(StickyUtils.dp2px(this,40))//设置悬浮标题高度
.setGroupTextColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))//设置标题文字颜色
.setGroupTextSize(StickyUtils.dp2px(this,20))//设置标题文字大小,可以传资源文件size
.setTextSideMargin(StickyUtils.dp2px(this,20))//设置标题的边距
//设置分割线相关
.setDivideColor(StickyUtils.getColor(this,R.color.colorPrimary))//分割线颜色
.setDivideHeight(StickyUtils.dp2px(this,2))//分割线高度
.setDivideLeftPadding(StickyUtils.dp2px(this,20))//分割线左侧间距
.setOnClickListener { position, id ->
//悬浮标题点击监听
Toast.makeText(this, lists[position].tag, Toast.LENGTH_SHORT).show()
}
.build()
上面可以设置悬浮内容,高度和分割线等,设置好以后调用 addItemDecration 就可以了,如下:
rv_list?.run {
layoutManager = LinearLayoutManager(this@LinearActivity)
adapter = mAdapter
addItemDecoration(itemDecoration)
}
mAdapter.setList(lists)
- 2.线性布局自定义悬浮
val itemDecoration = PowerfulStickyDecoration.Builder.init(object : PowerGroupListener{
override fun getGroupName(position: Int): String {
return if (lists.size > position && position > -1){
lists[position].tag
}else{
""
}
}
override fun getGroupView(position: Int): View {
val customView = layoutInflater.inflate(R.layout.item_sticky_custom_top,null,false)
//在这里获取悬浮布局,并设置数据等
if (lists.size > position && position > -1){
customView.tv_item_sticky_custom_top_title.text = lists[position].tag
}
return customView
}
}).setDivideColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))
.setDivideHeight(StickyUtils.dp2px(this, 1f))
.setOnClickListener { position, id ->
val datas = mAdapter.data
if (datas.size > position) {
Toast.makeText(this, "--${datas[position].tag}", Toast.LENGTH_SHORT).show()
}
}.build()
rv_list?.run {
layoutManager = LinearLayoutManager(this@LinearCustomActivity)
adapter = mAdapter
addItemDecoration(itemDecoration)
}
mAdapter.setList(lists)
- 3.网格布局
val gridLayoutManager = GridLayoutManager(this,2)
val size = resources.getDimensionPixelOffset(R.dimen.sp_14)
val itemDecoration = StickyDecoration.Builder.init {
if (lists.size > it && it > - 1){
lists[it].tag
}else ""
}.setGroupBackground(Color.parseColor("#07C0C2"))
//设置悬浮标题整体背景,颜色可以传资源文件StickyUtils.getColor(this,R.color.colorAccent)
.setGroupHeight(StickyUtils.dp2px(this,40))//设置悬浮标题高度
.setGroupTextColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))//设置标题文字颜色
.setGroupTextSize(StickyUtils.dp2px(this,20))//设置标题文字大小,可以传资源文件size
.setTextSideMargin(StickyUtils.dp2px(this,20))//设置标题的边距
//设置分割线相关
.setDivideColor(StickyUtils.getColor(this,R.color.colorPrimary))//分割线颜色
.setDivideHeight(StickyUtils.dp2px(this,2))//分割线高度
.setDivideLeftPadding(StickyUtils.dp2px(this,20))//分割线左侧间距
.resetSpan(rv_list,gridLayoutManager)
.build()
rv_list?.run {
layoutManager = gridLayoutManager
adapter = mAdapter
addItemDecoration(itemDecoration)
}
mAdapter.setList(lists)
4.网格布局自定义悬浮 ``` val lists = DataUtil.getCategoryList()
//lists.addAll(lists)
val gridLayoutManager = GridLayoutManager(this, 2)
val size = resources.getDimensionPixelOffset(R.dimen.sp_14)
val itemDecoration = PowerfulStickyDecoration.Builder.init(object : PowerGroupListener {
override fun getGroupName(position: Int): String {
return if (lists.size > position && position > -1){
lists[position].tag
}else{
""
}
}
//设置自定义悬浮布局
override fun getGroupView(position: Int): View {
val customView = layoutInflater.inflate(R.layout.item_sticky_custom_top,null,false)
//在这里获取悬浮布局,并设置数据等
if (lists.size > position && position > -1){
customView.tv_item_sticky_custom_top_title.text = lists[position].tag
}
return customView
}
})
//设置分割线
.setDivideColor(StickyUtils.getColor(this,R.color.colorPrimaryDark))
.setDivideHeight(StickyUtils.dp2px(this, 1f))
.setOnClickListener { position, id ->
val datas = mAdapter.data
if (datas.size > position) {
Toast.makeText(this, "--${datas[position].tag}", Toast.LENGTH_SHORT).show()
}
}.resetSpan(rv_grid_custom_lsit,gridLayoutManager)
.build()
// itemDecoration.resetSpan(rv_grid_custom_lsit, gridLayoutManager) rv_grid_custom_lsit?.run { layoutManager = gridLayoutManager adapter = mAdapter addItemDecoration(itemDecoration) } mAdapter.setList(lists) ```