Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Fix layout updates
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombo11 committed Jun 18, 2019
1 parent b547614 commit b3ce02d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.core:core-ktx:1.0.2'
implementation "androidx.recyclerview:recyclerview:1.0.0"

implementation project(":library")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package eu.acolombo.minimap.example
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import eu.acolombo.minimap.MinimapView.Companion.minimap
import eu.acolombo.minimap.example.data.Car
import eu.acolombo.minimap.example.data.Parking
import eu.acolombo.minimap.minimap
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -60,6 +60,9 @@ class MainActivity : AppCompatActivity() {
updateCounter(adapter.emptyParkingSpots)
removedLines++
if (removedLines == 4) removedLines = 0

// If we are still having problems in the future, make updateScaleFactor public and use it when notifying adapter changes like this:
// minimapView.updateScaleFactor(recyclerView)
}

private fun updateCounter(count: Int) {
Expand Down
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {

implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.core:core-ktx:1.0.2"

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Expand Down
20 changes: 8 additions & 12 deletions library/src/main/java/eu/acolombo/minimap/MinimapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.view.doOnLayout
import androidx.core.view.doOnNextLayout
import androidx.core.view.doOnPreDraw
import androidx.recyclerview.widget.RecyclerView

class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
Expand Down Expand Up @@ -52,13 +55,9 @@ class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSe

fun setRecyclerView(recyclerView: RecyclerView) {
// Wait for recyclerView to be measured before doing anything with the minimap
recyclerView.addLayoutChangeListenerHandler {
updateScaleFactor(this)
}
recyclerView.addLayoutChangeListener { doOnPreDraw { updateScaleFactor(recyclerView) } }

recyclerView.addScrollListener { dx, dy ->
if (this@MinimapView.visibility == View.VISIBLE) moveIndicator(dx, dy)
}
recyclerView.addScrollListener { dx, dy -> if (this@MinimapView.visibility == VISIBLE) moveIndicator(dx, dy) }
}

private fun updateScaleFactor(rv: RecyclerView) {
Expand Down Expand Up @@ -94,12 +93,9 @@ class MinimapView @JvmOverloads constructor(context: Context, attrs: AttributeSe
}
}

private fun View.updateVisibility() = if (this.shouldBeVisible()) {
visibility = this.visibility
this.visibility == View.VISIBLE
} else {
visibility = View.GONE
false
private fun View.updateVisibility() : Boolean {
if (!shouldBeVisible()) visibility = GONE
return visibility == VISIBLE
}

private fun View?.shouldBeVisible() = this != null && (scrollWidth > this.width || scrollHeight > this.height)
Expand Down
9 changes: 0 additions & 9 deletions library/src/main/java/eu/acolombo/minimap/RecyclerViewExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.acolombo.minimap

import android.os.Handler
import android.view.View
import android.view.ViewTreeObserver
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -14,14 +13,6 @@ fun RecyclerView.addScrollListener(func: (dx: Int, dy: Int) -> Unit) {
})
}

fun <T: View> T.addLayoutChangeListenerHandler(func: T.() -> Unit) {
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
Handler().post {
func()
}
}
}

fun <T: View> T.addLayoutChangeListener(func: T.() -> Unit) {
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
func()
Expand Down

0 comments on commit b3ce02d

Please sign in to comment.