Android RecycleView滑動(dòng)停止后自動(dòng)吸附效果的實(shí)現(xiàn)代碼(滑動(dòng)定位)
最近有個(gè)需求 要求列表 滑動(dòng)后第一條 需要和頂部對(duì)齊上網(wǎng)找了找 發(fā)現(xiàn) 官方支持 Recycle + LinearSnapHelper 可以實(shí)現(xiàn)但我實(shí)際操作加上后 發(fā)現(xiàn)會(huì)卡頓 滑動(dòng)卡頓 沒有以前那種流暢感了
想了想 算了 懶得看源碼 還是自己寫一個(gè)得了
效果圖 :
代碼如下 注釋很清楚了
package com.example.testapp import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.recyclerview.widget.LinearLayoutManagerimport androidx.recyclerview.widget.RecyclerViewimport com.example.testapp.code.note.JoinDataimport com.example.testapp.code.note.TheatreJoinerAdapterimport kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { //isUserControl 表示是否是 第二次定位滑動(dòng) @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)//判斷是否是自動(dòng)滾動(dòng)if (r.scrollState == RecyclerView.SCROLL_STATE_SETTLING && !isUserControl) {//自動(dòng)滾動(dòng) //滾動(dòng)幅度 在 -3 .. 3以內(nèi) 其實(shí)時(shí)接近停止了 慢速滑動(dòng)了 這時(shí)我們讓他停止 if (dy in -3..3) {//向下滾動(dòng) r.stopScroll() }} } override fun onScrollStateChanged(r: RecyclerView, newState: Int) {super.onScrollStateChanged(r, newState)if (newState == RecyclerView.SCROLL_STATE_IDLE) {//滑動(dòng)停止 if (!isUserControl) { rcy.postDelayed(runnable, 200)//200 毫秒延時(shí)任務(wù) }}if (r.scrollState != RecyclerView.SCROLL_STATE_SETTLING) {//非自動(dòng)滑動(dòng) isUserControl = false} } }) } private fun smoothScrollToPosition() { isUserControl = true val stickyInfoView = rcy.getChildAt(0) //獲取頭部View 第一個(gè)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))//二次滑動(dòng) } else {rcy.smoothScrollBy(0, bottom)//二次滑動(dòng) } } }}
結(jié)束
到此這篇關(guān)于Android RecycleView滑動(dòng)停止后自動(dòng)吸附效果的實(shí)現(xiàn)代碼(滑動(dòng)定位)的文章就介紹到這了,更多相關(guān)Android RecycleView滑動(dòng)定位內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python 的 __str__ 和 __repr__ 方法對(duì)比2. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法3. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)4. IntelliJ IDEA設(shè)置背景圖片的方法步驟5. docker /var/lib/docker/aufs/mnt 目錄清理方法6. Python TestSuite生成測(cè)試報(bào)告過程解析7. 學(xué)python最電腦配置有要求么8. JAMon(Java Application Monitor)備忘記9. Python Scrapy多頁(yè)數(shù)據(jù)爬取實(shí)現(xiàn)過程解析10. Python OpenCV去除字母后面的雜線操作
