Skip to content

Instantly share code, notes, and snippets.

@lukelorusso
Last active August 5, 2020 17:02
Show Gist options
  • Save lukelorusso/86e0635c15e761074efd108f40fc8a30 to your computer and use it in GitHub Desktop.
Save lukelorusso/86e0635c15e761074efd108f40fc8a30 to your computer and use it in GitHub Desktop.
package com.lukelorusso.toomuchofscrolling
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(R.layout.activity_main) {
companion object {
private val base10List = listOf("0", ..., "15") // TODO: fill it :)
private val binaryList = listOf("0", ..., "1111") // TODO: fill it :)
private val hexadecimalList = listOf("0", ..., "F") // TODO: fill it :)
}
// Properties
private val base10Adapter = Base10Adapter()
private val binaryAdapter = BinaryAdapter()
private val hexadecimalAdapter = HexadecimalAdapter()
private var base10Listener: RecyclerView.OnScrollListener? = null
private var binaryListener: RecyclerView.OnScrollListener? = null
private var hexadecimalListener: RecyclerView.OnScrollListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initializeActivity(savedInstanceState)
}
private fun initializeActivity(savedInstanceState: Bundle?) {
if (savedInstanceState == null) {
initView()
}
}
private fun initView() {
base10RecyclerView.adapter = base10Adapter.apply { data = base10List }
binaryRecyclerView.adapter = binaryAdapter.apply { data = binaryList }
hexadecimalRecyclerView.adapter = hexadecimalAdapter.apply { data = hexadecimalList }
bind()
bindButton?.setOnClickListener { bind() }
unbindButton?.setOnClickListener { unbind() }
}
private fun bind() {
bindButton?.isEnabled = false
unbindButton?.isEnabled = false
base10Listener = base10RecyclerView
.bindScrollTo(binaryRecyclerView, hexadecimalRecyclerView)
binaryListener = binaryRecyclerView
.bindScrollTo(base10RecyclerView, hexadecimalRecyclerView)
hexadecimalListener = hexadecimalRecyclerView
.bindScrollTo(base10RecyclerView, binaryRecyclerView)
bindButton?.isEnabled = false
unbindButton?.isEnabled = true
}
private fun unbind() {
bindButton?.isEnabled = false
unbindButton?.isEnabled = false
base10Listener?.also { listener ->
base10RecyclerView.removeOnScrollListener(listener)
}
binaryListener?.also { listener ->
binaryRecyclerView.removeOnScrollListener(listener)
}
hexadecimalListener?.also { listener ->
hexadecimalRecyclerView.removeOnScrollListener(listener)
}
bindButton?.isEnabled = true
unbindButton?.isEnabled = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment