Skip to content

Commit

Permalink
[修复] 新番顶部列表滑动不流畅的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
YenalyLiew committed Oct 3, 2024
1 parent 55047da commit 16ed3fe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package com.yenaly.han1meviewer.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.annotation.OptIn
Expand All @@ -25,7 +23,6 @@ import androidx.lifecycle.repeatOnLifecycle
import androidx.palette.graphics.Palette
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.viewpager2.widget.ViewPager2
import coil.imageLoader
import coil.load
Expand All @@ -50,7 +47,6 @@ import com.yenaly.han1meviewer.util.colorTransition
import com.yenaly.han1meviewer.util.toColorStateList
import com.yenaly.yenaly_libs.base.YenalyActivity
import com.yenaly.yenaly_libs.utils.appScreenWidth
import com.yenaly.yenaly_libs.utils.dp
import com.yenaly.yenaly_libs.utils.getThemeColor
import com.yenaly.yenaly_libs.utils.startActivity
import com.yenaly.yenaly_libs.utils.unsafeLazy
Expand Down Expand Up @@ -166,28 +162,15 @@ class PreviewActivity : YenalyActivity<ActivityPreviewBinding, PreviewViewModel>
binding.rvTourSimplified.apply {
layoutManager = tourLayoutManager
adapter = tourSimplifiedAdapter
addItemDecoration(object : ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
val position = parent.getChildViewHolder(view).bindingAdapterPosition
if (position == 0 || position == state.itemCount - 1) {
val elementWidth = resources.getDimension(
R.dimen.video_cover_simplified_width_small
)
val elementMargin = 4.dp
val padding = appScreenWidth / 2f - elementWidth / 2f - elementMargin
if (position == 0) {
outRect.left = padding.toInt()
} else {
outRect.right = padding.toInt()
}
}
}
})
clipToPadding = false
ViewCompat.setOnApplyWindowInsetsListener(this) { view, _ ->
val elementWidth = view.resources.getDimension(
R.dimen.video_cover_simplified_width_small
)
val padding = appScreenWidth / 2f - elementWidth / 2f
view.updatePadding(left = padding.toInt(), right = padding.toInt())
WindowInsetsCompat.CONSUMED
}
linearSnapHelper.attachToRecyclerView(this)
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ open class CenterLinearLayoutManager : LinearLayoutManager {
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int)
: super(context, attrs, defStyleAttr, defStyleRes)

// Shrink the cards around the center up to 15%
private val mShrinkAmount = 0.15f
companion object {
// Shrink the cards around the center up to 15%
private const val SHRINK_AMOUNT = 0.15f

// The cards will be at 15% when they are 80% of the way between the
// center and the edge.
private val mShrinkDistance = 0.8f
// The cards will be at 15% when they are 80% of the way between the
// center and the edge.
private const val SHRINK_DISTANCE = 0.8f
}

override fun smoothScrollToPosition(
recyclerView: RecyclerView,
Expand All @@ -43,12 +45,12 @@ open class CenterLinearLayoutManager : LinearLayoutManager {
val scrolled = super.scrollHorizontallyBy(dx, recycler, state)
val midpoint = width / 2f
val d0 = 0f
val d1: Float = mShrinkDistance * midpoint
val d1: Float = SHRINK_DISTANCE * midpoint
val s0 = 1f
val s1: Float = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedRight(child!!) + getDecoratedLeft(child)) / 2f
val s1: Float = 1f - SHRINK_AMOUNT
for (i in 0..<childCount) {
val child = getChildAt(i) ?: continue
val childMidpoint = (getDecoratedRight(child) + getDecoratedLeft(child)) / 2f
val d = d1.coerceAtMost(abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
Expand All @@ -69,12 +71,12 @@ open class CenterLinearLayoutManager : LinearLayoutManager {
val scrolled = super.scrollVerticallyBy(dy, recycler, state)
val midpoint = height / 2f
val d0 = 0f
val d1 = mShrinkDistance * midpoint
val d1 = SHRINK_DISTANCE * midpoint
val s0 = 1f
val s1 = 1f - mShrinkAmount
for (i in 0 until childCount) {
val child = getChildAt(i)
val childMidpoint = (getDecoratedBottom(child!!) + getDecoratedTop(child)) / 2f
val s1 = 1f - SHRINK_AMOUNT
for (i in 0..<childCount) {
val child = getChildAt(i) ?: continue
val childMidpoint = (getDecoratedBottom(child) + getDecoratedTop(child)) / 2f
val d = d1.coerceAtMost(abs(midpoint - childMidpoint))
val scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0)
child.scaleX = scale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import com.yenaly.han1meviewer.logic.NetworkRepo
import com.yenaly.han1meviewer.logic.entity.SearchHistoryEntity
import com.yenaly.han1meviewer.logic.model.HanimeInfo
import com.yenaly.han1meviewer.logic.model.SearchOption
import com.yenaly.han1meviewer.logic.model.SearchTag
import com.yenaly.han1meviewer.logic.state.PageLoadingState
import com.yenaly.han1meviewer.logic.state.WebsiteState
import com.yenaly.han1meviewer.util.loadAssetAs
import com.yenaly.yenaly_libs.base.YenalyViewModel
import com.yenaly.yenaly_libs.utils.unsafeLazy
Expand Down Expand Up @@ -80,10 +78,6 @@ class SearchViewModel(application: Application) : YenalyViewModel(application) {
private val _searchFlow = MutableStateFlow(emptyList<HanimeInfo>())
val searchFlow = _searchFlow.asStateFlow()

private val _searchTagFlow =
MutableStateFlow<WebsiteState<SearchTag>>(WebsiteState.Loading)
val searchTagFlow = _searchTagFlow.asStateFlow()

fun clearHanimeSearchResult() = _searchStateFlow.update { PageLoadingState.Loading }

fun getHanimeSearchResult(
Expand Down

0 comments on commit 16ed3fe

Please sign in to comment.