Android RecycleView滑动停止后自动吸附效果的实现代码(滑动定位)
作者:叫我大红领巾 时间:2023-06-05 21:23:40
最近有个需求 要求列表 滑动后第一条 需要和顶部对齐
上网找了找 发现 官方支持 Recycle + LinearSnapHelper 可以实现
但我实际操作加上后 发现会卡顿 滑动卡顿 没有以前那种流畅感了
想了想 算了 懒得看源码 还是自己写一个得了
效果图 :
代码如下 注释很清楚了
package com.example.testapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.testapp.code.note.JoinData
import com.example.testapp.code.note.TheatreJoinerAdapter
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
//isUserControl 表示是否是 第二次定位滑动
@Volatile
private var isUserControl = false
var runnable = Runnable {
smoothScrollToPosition()//处理rcy定位
}
val list = arrayListOf<JoinData>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for (i in 0..50) {
list.add(JoinData("小名${i}", i))
}
rcy.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
var adapter = TheatreJoinerAdapter(this, list)
rcy.adapter = adapter
rcy.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(r: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(r, dx, dy)
//判断是否是自动滚动
if (r.scrollState == RecyclerView.SCROLL_STATE_SETTLING && !isUserControl) {//自动滚动
//滚动幅度 在 -3 .. 3以内 其实时接近停止了 慢速滑动了 这时我们让他停止
if (dy in -3..3) {//向下滚动
r.stopScroll()
}
}
}
override fun onScrollStateChanged(r: RecyclerView, newState: Int) {
super.onScrollStateChanged(r, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {//滑动停止
if (!isUserControl) {
rcy.postDelayed(runnable, 200)//200 毫秒延时任务
}
}
if (r.scrollState != RecyclerView.SCROLL_STATE_SETTLING) {//非自动滑动
isUserControl = false
}
}
})
}
private fun smoothScrollToPosition() {
isUserControl = true
val stickyInfoView = rcy.getChildAt(0) //获取头部View 第一个view
val bottom = stickyInfoView.bottom//获取view底部到rcy的顶部高度
val height = stickyInfoView.measuredHeight//获取view高度
if (bottom != height) {//去除正好停在正好的位置的情况
if (bottom >= (height / 2)) {//判断view在上一半还是在下一半
rcy.smoothScrollBy(0, -(height - bottom))//二次滑动
} else {
rcy.smoothScrollBy(0, bottom)//二次滑动
}
}
}
}
结束
来源:https://blog.csdn.net/qq_42535851/article/details/109319524
标签:Android,RecycleView,滑动定位,吸附
0
投稿
猜你喜欢
Android studio设计简易计算器
2023-01-26 22:21:40
Spring Boot实现分布式锁的自动释放的示例代码
2023-10-17 11:06:24
60条Android开发注意事项与经验总结
2021-12-07 11:55:16
Java集合使用 Iterator 删除元素
2022-02-25 12:32:44
MVPXlistView展示上拉下拉效果
2022-10-30 10:45:38
java初学者必须理解这几个问题
2023-04-07 14:22:36
Spring Cloud Eureka(全面解析) 大白话
2022-11-12 22:43:02
Android实现页面滑动切换动画
2023-12-26 11:47:54
C#对Word文档的创建、插入表格、设置样式等操作实例
2021-10-24 23:13:46
Java多线程下载网图的完整案例
2022-10-17 23:59:02
详细解读C++编程中的匿名类类型和位域
2023-11-02 23:08:18
Spring的AOP极简入门
2023-07-10 22:24:32
Spring Boot如何使用Spring Security进行安全控制
2022-03-26 03:59:41
Struts2拦截器登录验证实例
2021-10-26 06:47:04
全面解析Java中的引用类型
2022-07-15 23:48:07
详解Spring Security如何在权限中使用通配符
2023-04-17 23:41:54
C#实现餐厅管理系统
2023-11-27 16:05:05
Android 动态菜单实现实例代码
2023-05-19 16:24:58
Android五大布局与实际应用详解
2022-01-30 05:21:36
C#关于Func和Action委托的介绍详解
2022-10-13 04:43:14