Skip to content

Commit

Permalink
layouting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kroegerama committed Apr 22, 2021
1 parent 3d3cd56 commit 3dff444
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/src/main/kotlin/com/kroegerama/reswista/ui/AcMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.kroegerama.reswista.ui

import androidx.activity.viewModels
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.navOptions
import androidx.navigation.ui.setupWithNavController
import com.kroegerama.kaiteki.baseui.ViewBindingActivity
import com.kroegerama.kaiteki.onClick
import com.kroegerama.reswista.R
import com.kroegerama.reswista.controller.Persistence
import com.kroegerama.reswista.databinding.AcMainBinding
Expand All @@ -27,5 +29,9 @@ class AcMain : ViewBindingActivity<AcMainBinding>(AcMainBinding::inflate) {
val navController = (supportFragmentManager.findFragmentById(R.id.navHost) as NavHostFragment).navController
setSupportActionBar(toolbar)
toolbar.setupWithNavController(navController)

btnSwitchFrag.onClick {
navController.navigate(R.id.fragStart)//, null, navOptions { popUpTo(R.id.main_nav) { inclusive = true } })
}
}
}
11 changes: 10 additions & 1 deletion app/src/main/kotlin/com/kroegerama/reswista/ui/FragStart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import android.graphics.Color
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.transition.MaterialFadeThrough
import com.kroegerama.kaiteki.baseui.ViewBindingFragment
import com.kroegerama.kaiteki.onClick
import com.kroegerama.kaiteki.postponeUntilLayout
import com.kroegerama.kaiteki.recyclerview.ViewBindingBaseViewHolder
import com.kroegerama.reswista.R
import com.kroegerama.reswista.StackDirection
Expand All @@ -28,7 +30,14 @@ class FragStart : ViewBindingFragment<FragStartBinding>(

private val itemAdapter by lazy { ItemAdapter() }

override fun prepare() {
enterTransition = MaterialFadeThrough()
exitTransition = MaterialFadeThrough()
}

override fun FragStartBinding.setupGUI() {
postponeUntilLayout(recycler)

val config = SwiperConfig(
showCount = 5,
swipeDirections = SwipeDirection.LEFT or SwipeDirection.RIGHT or SwipeDirection.UP,
Expand Down Expand Up @@ -58,7 +67,7 @@ class FragStart : ViewBindingFragment<FragStartBinding>(
val startColor = requireContext().getColor(R.color.primary)
val endColor = requireContext().getColor(R.color.secondary)
val evaluator = ArgbEvaluator()
val count = 10
val count = 3

fun getColor(pos: Int): Int {
val fraction = (pos - 1f) / count
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/layout/ac_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
android:id="@+id/navHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav" />

<com.google.android.material.button.MaterialButton
android:id="@+id/btnSwitchFrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

</LinearLayout>
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/ProjectInfo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object P {
const val projectVersion = "0.9.2"
const val projectVersion = "0.9.3"

const val projectGroupId = "com.kroegerama"
const val libraryArtifactId = "recycler-swipe-stack"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.kroegerama.kaiteki.scale
import timber.log.Timber

class StackLayoutManager(
private val config: SwiperConfig = SwiperConfig()
) : RecyclerView.LayoutManager() {

override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams =
RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
detachAndScrapAttachedViews(recycler)
val showCount = config.showCount

val itemCount = state.itemCount

if (itemCount > showCount) {
for (pos in showCount downTo 0) {
val layout = recycler.getViewForPosition(pos)
addAndMeasureView(layout)
layout.translationZ = (-pos).toFloat()

when {
pos == showCount -> {
Expand All @@ -37,6 +41,7 @@ class StackLayoutManager(
for (pos in itemCount - 1 downTo 0) {
val layout = recycler.getViewForPosition(pos)
addAndMeasureView(layout)
layout.translationZ = (-pos).toFloat()

when {
pos > 0 -> {
Expand Down Expand Up @@ -66,15 +71,23 @@ class StackLayoutManager(
}

private fun View.resetTransitions() {
clearAnimation()
scale(1f)
translationX = 0f
translationY = 0f
animate().alpha(1f)
}

private fun View.scaleAndTranslateForPosition(pos: Int) {
clearAnimation()
scale(1f - pos * config.itemScale)
val itemTranslate = config.itemTranslate
when (config.stackDirection) {
StackDirection.Left -> translationX = -pos * measuredWidth * itemTranslate
StackDirection.Up -> translationY = -pos * measuredHeight * itemTranslate
StackDirection.Right -> translationX = pos * measuredWidth * itemTranslate
StackDirection.Down -> translationY = pos * measuredHeight * itemTranslate
}
}

private fun View.scaleAndTranslateForPositionAnimated(pos: Int) {
animate().apply {
val scale = 1f - pos * config.itemScale
scaleX(scale)
Expand Down
1 change: 0 additions & 1 deletion library/src/main/kotlin/com/kroegerama/reswista/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ fun RecyclerView.setupStack(
listener: SwipeListener,
block: (RecyclerView.() -> Unit)? = null
) {
itemAnimator = null
layoutManager = StackLayoutManager(config)
ItemTouchHelper(StackSwipeTouchHelperCallback(listener, config)).attachToRecyclerView(this)
block?.invoke(this)
Expand Down

0 comments on commit 3dff444

Please sign in to comment.