mirror of
https://github.com/lizongying/my-tv.git
synced 2026-11-25 12:56:33 +08:00
optimize memu
This commit is contained in:
@@ -2,21 +2,24 @@ package com.lizongying.mytv
|
||||
|
||||
import android.graphics.Color
|
||||
import android.view.ContextThemeWrapper
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.animation.Animation
|
||||
import android.view.animation.ScaleAnimation
|
||||
import android.widget.ImageView
|
||||
import androidx.leanback.widget.ImageCardView
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.lizongying.mytv.models.TVListViewModel
|
||||
import com.lizongying.mytv.models.TVViewModel
|
||||
|
||||
|
||||
class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel: TVListViewModel) :
|
||||
class CardAdapter(
|
||||
private val recyclerView: RecyclerView,
|
||||
private val owner: LifecycleOwner,
|
||||
private var tvListViewModel: TVListViewModel
|
||||
) :
|
||||
RecyclerView.Adapter<CardAdapter.ViewHolder>() {
|
||||
|
||||
private var listener: ItemListener? = null
|
||||
@@ -24,22 +27,32 @@ class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val cardView = object :
|
||||
ImageCardView(ContextThemeWrapper(parent.context, R.style.CustomImageCardTheme)) {}
|
||||
startScaleAnimation(cardView, 1.0f, 0.9f)
|
||||
cardView.isFocusable = true
|
||||
cardView.isFocusableInTouchMode = true
|
||||
|
||||
return ViewHolder(cardView)
|
||||
}
|
||||
|
||||
private fun startScaleAnimation(view: View, fromScale: Float, toScale: Float) {
|
||||
private fun startScaleAnimation(view: View, fromScale: Float, toScale: Float, duration: Long) {
|
||||
val scaleAnimation = ScaleAnimation(
|
||||
fromScale, toScale,
|
||||
fromScale, toScale,
|
||||
ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
|
||||
ScaleAnimation.RELATIVE_TO_SELF, 0.5f
|
||||
)
|
||||
scaleAnimation.duration = 200
|
||||
scaleAnimation.duration = duration
|
||||
scaleAnimation.fillAfter = true
|
||||
scaleAnimation.setAnimationListener(object : Animation.AnimationListener {
|
||||
override fun onAnimationStart(animation: Animation?) {
|
||||
}
|
||||
|
||||
override fun onAnimationEnd(animation: Animation?) {
|
||||
view.tag = toScale
|
||||
}
|
||||
|
||||
override fun onAnimationRepeat(animation: Animation?) {
|
||||
}
|
||||
})
|
||||
|
||||
view.startAnimation(scaleAnimation)
|
||||
}
|
||||
@@ -50,12 +63,20 @@ class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel
|
||||
val tvViewModel = item as TVViewModel
|
||||
val cardView = viewHolder.view as ImageCardView
|
||||
|
||||
val onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
|
||||
startScaleAnimation(cardView, 1.0f, 0.9f, 0)
|
||||
|
||||
val onFocusChangeListener = View.OnFocusChangeListener { view, hasFocus ->
|
||||
listener?.onItemFocusChange(item, hasFocus)
|
||||
|
||||
if (hasFocus) {
|
||||
startScaleAnimation(cardView, 0.9f, 1.0f)
|
||||
} else {
|
||||
startScaleAnimation(cardView, 1.0f, 0.9f)
|
||||
startScaleAnimation(cardView, 0.9f, 1.0f, 200)
|
||||
|
||||
for (i in 0 until recyclerView.childCount) {
|
||||
val v = recyclerView.getChildAt(i)
|
||||
if (v != view && v.tag != 0.9f) {
|
||||
startScaleAnimation(v, 1.0f, 0.9f, 200)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +87,6 @@ class CardAdapter(private val owner: LifecycleOwner, private var tvListViewModel
|
||||
}
|
||||
|
||||
cardView.titleText = tvViewModel.title.value
|
||||
cardView.tag = tvViewModel.videoUrl.value
|
||||
|
||||
when (tvViewModel.title.value) {
|
||||
"CCTV8K 超高清" -> Glide.with(viewHolder.view.context)
|
||||
|
||||
Reference in New Issue
Block a user