Android CollapsingToolbarLayout collapse Listener

后端 未结 12 1169
情歌与酒
情歌与酒 2020-12-12 11:37

I am using CollapsingToolBarLayout alongside with AppBarLayout and CoordinatorLayout, and they are working Fine altogether. I set my <

12条回答
  •  鱼传尺愫
    2020-12-12 12:37

    Here is the solution in Kotlin:

    abstract class AppBarStateChangeListener : OnOffsetChangedListener {
        enum class State {
            EXPANDED, COLLAPSED, IDLE
        }
    
        private var mCurrentState =
            State.IDLE
    
        override fun onOffsetChanged(appBarLayout: AppBarLayout, i: Int) {
            mCurrentState = if (i == 0) {
                if (mCurrentState != State.EXPANDED) {
                    onStateChanged(appBarLayout, State.EXPANDED)
                }
                State.EXPANDED
            } else if (Math.abs(i) >= appBarLayout.totalScrollRange) {
                if (mCurrentState != State.COLLAPSED) {
                    onStateChanged(appBarLayout, State.COLLAPSED)
                }
                State.COLLAPSED
            } else {
                if (mCurrentState != State.IDLE) {
                    onStateChanged(appBarLayout, State.IDLE)
                }
                State.IDLE
            }
        }
    
        abstract fun onStateChanged(
            appBarLayout: AppBarLayout?,
            state: State?
        )
    }
    

    Here is the listener:

      appbar.addOnOffsetChangedListener(object : AppBarStateChangeListener() {
            override fun onStateChanged(
                appBarLayout: AppBarLayout?,
                state: State?
            ) {
                if(state == State.COLLAPSED){
                    LayoutBottom.visibility = View.GONE
                }else if(state == State.EXPANDED){
                    LayoutBottom.visibility = View.VISIBLE
                }
            }
        })
    

提交回复
热议问题