remove the static variables from MyScalableRecyclerView

This commit is contained in:
tibbi 2017-06-20 21:02:58 +02:00
parent aed1d64e43
commit c6066bcdc4
3 changed files with 47 additions and 24 deletions

View File

@ -134,7 +134,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
mStoredAnimateGifs = config.animateGifs mStoredAnimateGifs = config.animateGifs
mStoredCropThumbnails = config.cropThumbnails mStoredCropThumbnails = config.cropThumbnails
mStoredScrollHorizontally = config.scrollHorizontally mStoredScrollHorizontally = config.scrollHorizontally
MyScalableRecyclerView.mListener = null directories_grid.listener = null
mLastMediaHandler.removeCallbacksAndMessages(null) mLastMediaHandler.removeCallbacksAndMessages(null)
} }
@ -242,7 +242,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
directories_grid.isDragSelectionEnabled = true directories_grid.isDragSelectionEnabled = true
directories_grid.isZoomingEnabled = true directories_grid.isZoomingEnabled = true
layoutManager.spanCount = config.dirColumnCnt layoutManager.spanCount = config.dirColumnCnt
MyScalableRecyclerView.mListener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener { directories_grid.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
reduceColumnCount() reduceColumnCount()

View File

@ -102,7 +102,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mStoredAnimateGifs = config.animateGifs mStoredAnimateGifs = config.animateGifs
mStoredCropThumbnails = config.cropThumbnails mStoredCropThumbnails = config.cropThumbnails
mStoredScrollHorizontally = config.scrollHorizontally mStoredScrollHorizontally = config.scrollHorizontally
MyScalableRecyclerView.mListener = null media_grid.listener = null
mLastMediaHandler.removeCallbacksAndMessages(null) mLastMediaHandler.removeCallbacksAndMessages(null)
} }
@ -318,7 +318,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
media_grid.isDragSelectionEnabled = true media_grid.isDragSelectionEnabled = true
media_grid.isZoomingEnabled = true media_grid.isZoomingEnabled = true
layoutManager.spanCount = config.mediaColumnCnt layoutManager.spanCount = config.mediaColumnCnt
MyScalableRecyclerView.mListener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener { media_grid.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener {
override fun zoomIn() { override fun zoomIn() {
if (layoutManager.spanCount > 1) { if (layoutManager.spanCount > 1) {
reduceColumnCount() reduceColumnCount()

View File

@ -13,6 +13,7 @@ class MyScalableRecyclerView : RecyclerView {
private val AUTO_SCROLL_DELAY = 25L private val AUTO_SCROLL_DELAY = 25L
var isZoomingEnabled = false var isZoomingEnabled = false
var isDragSelectionEnabled = false var isDragSelectionEnabled = false
var listener: MyScalableRecyclerViewListener? = null
private var mScaleDetector: ScaleGestureDetector private var mScaleDetector: ScaleGestureDetector
@ -35,11 +36,8 @@ class MyScalableRecyclerView : RecyclerView {
private var inTopHotspot = false private var inTopHotspot = false
private var inBottomHotspot = false private var inBottomHotspot = false
companion object { private var currScaleFactor = 1.0f
var mListener: MyScalableRecyclerViewListener? = null private var lastUp = 0L // allow only pinch zoom, not double tap
var mCurrScaleFactor = 1.0f
var mLastUp = 0L // allow only pinch zoom, not double tap
}
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
@ -47,7 +45,20 @@ class MyScalableRecyclerView : RecyclerView {
init { init {
hotspotHeight = context.resources.getDimensionPixelSize(R.dimen.dragselect_hotspot_height) hotspotHeight = context.resources.getDimensionPixelSize(R.dimen.dragselect_hotspot_height)
mScaleDetector = ScaleGestureDetector(context, GestureListener())
val gestureListener = object : MyGestureListenerInterface {
override fun getLastUp() = lastUp
override fun getScaleFactor() = currScaleFactor
override fun setScaleFactor(value: Float) {
currScaleFactor = value
}
override fun getMainListener() = listener
}
mScaleDetector = ScaleGestureDetector(context, GestureListener(gestureListener))
} }
override fun onMeasure(widthSpec: Int, heightSpec: Int) { override fun onMeasure(widthSpec: Int, heightSpec: Int) {
@ -83,8 +94,8 @@ class MyScalableRecyclerView : RecyclerView {
inTopHotspot = false inTopHotspot = false
inBottomHotspot = false inBottomHotspot = false
autoScrollHandler.removeCallbacks(autoScrollRunnable) autoScrollHandler.removeCallbacks(autoScrollRunnable)
mCurrScaleFactor = 1.0f currScaleFactor = 1.0f
mLastUp = System.currentTimeMillis() lastUp = System.currentTimeMillis()
return true return true
} }
@ -139,7 +150,7 @@ class MyScalableRecyclerView : RecyclerView {
minReached = lastDraggedIndex minReached = lastDraggedIndex
} }
mListener?.selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) listener?.selectRange(initialSelection, lastDraggedIndex, minReached, maxReached)
if (initialSelection == lastDraggedIndex) { if (initialSelection == lastDraggedIndex) {
minReached = lastDraggedIndex minReached = lastDraggedIndex
@ -166,7 +177,7 @@ class MyScalableRecyclerView : RecyclerView {
maxReached = -1 maxReached = -1
this.initialSelection = initialSelection this.initialSelection = initialSelection
dragSelectActive = true dragSelectActive = true
mListener?.selectItem(initialSelection) listener?.selectItem(initialSelection)
} }
private fun getItemPosition(e: MotionEvent): Int { private fun getItemPosition(e: MotionEvent): Int {
@ -181,21 +192,23 @@ class MyScalableRecyclerView : RecyclerView {
} }
class GestureListener : ScaleGestureDetector.SimpleOnScaleGestureListener() { class GestureListener(val gestureListener: MyGestureListenerInterface) : ScaleGestureDetector.SimpleOnScaleGestureListener() {
private val ZOOM_IN_THRESHOLD = -0.4f private val ZOOM_IN_THRESHOLD = -0.4f
private val ZOOM_OUT_THRESHOLD = 0.15f private val ZOOM_OUT_THRESHOLD = 0.15f
override fun onScale(detector: ScaleGestureDetector): Boolean { override fun onScale(detector: ScaleGestureDetector): Boolean {
if (System.currentTimeMillis() - mLastUp < 1000) gestureListener.apply {
return false if (System.currentTimeMillis() - getLastUp() < 1000)
return false
val diff = mCurrScaleFactor - detector.scaleFactor val diff = getScaleFactor() - detector.scaleFactor
if (diff < ZOOM_IN_THRESHOLD && mCurrScaleFactor == 1.0f) { if (diff < ZOOM_IN_THRESHOLD && getScaleFactor() == 1.0f) {
mListener?.zoomIn() getMainListener()?.zoomIn()
mCurrScaleFactor = detector.scaleFactor setScaleFactor(detector.scaleFactor)
} else if (diff > ZOOM_OUT_THRESHOLD && mCurrScaleFactor == 1.0f) { } else if (diff > ZOOM_OUT_THRESHOLD && getScaleFactor() == 1.0f) {
mListener?.zoomOut() getMainListener()?.zoomOut()
mCurrScaleFactor = detector.scaleFactor setScaleFactor(detector.scaleFactor)
}
} }
return false return false
} }
@ -210,4 +223,14 @@ class MyScalableRecyclerView : RecyclerView {
fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int)
} }
interface MyGestureListenerInterface {
fun getLastUp(): Long
fun getScaleFactor(): Float
fun setScaleFactor(value: Float)
fun getMainListener(): MyScalableRecyclerViewListener?
}
} }