This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mockito.BDDMockito | |
// Use just like a normal .willThrow: | |
// ``` | |
// given(api.fetch()).willThrowUnchecked(Exception("First error"), Exception("Second error")) | |
// ``` | |
fun <T> BDDMockito.BDDMyOngoingStubbing<T>.willThrowUnchecked(vararg throwables: Throwable) { | |
var invocationNumber = 0 | |
this.willAnswer { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.support.v4.view.ViewPager | |
import android.util.AttributeSet | |
internal class HeightWrappingViewPager @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) | |
: ViewPager(context, attrs) { | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
var heightMeasureSpecModif = heightMeasureSpec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var hasInitParentDimensions = false | |
private var maxImageWidth: Int = 0 | |
private var maxImageHeight: Int = 0 | |
private var maxImageAspectRatio: Float = 1f | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH { | |
if (!hasInitParentDimensions) { | |
maxImageWidth = parent.width | |
maxImageHeight = parent.height | |
maxImageAspectRatio = maxImageWidth.toFloat() / maxImageHeight.toFloat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBindViewHolder(vh: VH, position: Int) { | |
val image = images[position] | |
// Resize view to respect aspect ratio | |
val imageAspectRatio = image.aspectRatio | |
val targetImageWidth: Int = | |
if (imageAspectRatio < maxImageAspectRatio) { | |
// Tall image: height = max, width adjusts | |
(maxImageHeight * imageAspectRatio).roundToInt() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BoundsOffsetDecoration : ItemDecoration() { | |
override fun getItemOffsets(outRect: Rect, | |
view: View, | |
parent: RecyclerView, | |
state: RecyclerView.State) { | |
super.getItemOffsets(outRect, view, parent, state) | |
val itemPosition = parent.getChildAdapterPosition(view) | |
// It is crucial to refer to layoutParams.width |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun initRecyclerViewPosition(position: Int) { | |
// This initial scroll will be slightly off because it doesn't | |
// respect the SnapHelper. Do it anyway so that the target view | |
// is laid out, then adjust onPreDraw. | |
layoutManager.scrollToPosition(position) | |
recyclerView.doOnPreDraw { | |
val targetView = layoutManager.findViewByPosition(position) | |
?: return@doOnPreDraw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class ProminentLayoutManager( | |
context: Context, | |
private val minScaleDistanceFactor: Float = 1.5f, | |
private val scaleDownBy: Float = 0.5f | |
) : LinearLayoutManager(context, HORIZONTAL, false) { | |
override fun onLayoutCompleted(state: RecyclerView.State?) = | |
super.onLayoutCompleted(state).also { scaleChildren() } | |
override fun scrollHorizontallyBy( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val translationDirection = if (childCenter > containerCenter) -1 else 1 | |
val translationXFromScale = translationDirection * child.width * (1 - scale) / 2f | |
child.translationX = translationXFromScale |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var translationXForward = 0f | |
for (i in 0 until childCount) { | |
val translationXFromScale = ... // like before | |
child.translationX = translationXForward + translationXFromScale | |
translationXForward = 0f | |
if (translationXFromScale > 0 && i >= 1) { | |
// Edit previous child |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vh.imageView.setOnClickListener { | |
val rv = vh.imageView.parent as RecyclerView | |
rv.smoothScrollToCenteredPosition(position) | |
} | |
fun RecyclerView.smoothScrollToCenteredPosition(position: Int) { | |
val smoothScroller = object : LinearSmoothScroller(context) { | |
override fun calculateDxToMakeVisible(view: View?, | |
snapPref: Int): Int { |
OlderNewer