美文网首页Android开发
RecyclerView滚动监听处理标题栏渐变,tab吸顶/悬停

RecyclerView滚动监听处理标题栏渐变,tab吸顶/悬停

作者: 咚咚_Coding | 来源:发表于2021-03-29 11:41 被阅读0次

    一、滚动三种状态展示,如下

    1.刚进来标题栏透明
    2.上滑标题栏渐变
    3.上滑标题栏白色,蓝色Tab吸顶

    二、看代码

    原理:拿到内容中蓝色tab.getTop的高度
        mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                verScroll += dy
                if (verScroll == 0.0f || verScroll < mContentTabTopHeight) {
                    val scollYDistance = verScroll / mContentTabTopHeight
                    Log.d("onScrolled", "scollYDistance........$scollYDistance")
                    tv_head.setTextColor(argbEvaluator.evaluate(scollYDistance, Color.TRANSPARENT, Color.parseColor("#333333")) as Int)
                    tv_head.setBackgroundColor(argbEvaluator.evaluate(scollYDistance, Color.TRANSPARENT, Color.WHITE) as Int)
                    tv_head_right.setBackgroundColor(Color.TRANSPARENT)
                    isChange = true
                    if (headTopBar.visibility == View.VISIBLE)
                        headTopBar.visibility = View.GONE
                } else {
                    Log.d("onScrolled", "verScroll........$verScroll")
                    if (isChange) {
                        isChange = false
                        if (headTopBar.visibility == View.GONE) {
                            headTopBar.visibility = View.VISIBLE
                        }
                        tv_head.run {
                            tv_head.setBackgroundColor(Color.WHITE)
                            tv_head.setTextColor(Color.parseColor("#333333"))
                        }
                        tv_head_right.run {
                            tv_head_right.setBackgroundColor(Color.WHITE)
                        }
                    }
                    val position: Int? = layoutManager?.findFirstVisibleItemPosition()
                    if (currPos != position) {
                        currPos = position
                        tv_head_right.text = "$position"
                    }
                }
            }
        })

    相关文章

      网友评论

        本文标题:RecyclerView滚动监听处理标题栏渐变,tab吸顶/悬停

        本文链接:https://www.haomeiwen.com/subject/pgtbhltx.html