mirror of https://github.com/lizongying/my-tv.git
parent
8b33c7c94c
commit
a4da01adf4
@ -1,82 +0,0 @@ |
||||
package com.lizongying.mytv |
||||
|
||||
import android.graphics.Color |
||||
import android.view.ContextThemeWrapper |
||||
import android.view.ViewGroup |
||||
import android.widget.ImageView |
||||
import androidx.leanback.widget.ImageCardView |
||||
import androidx.leanback.widget.Presenter |
||||
import androidx.lifecycle.LifecycleOwner |
||||
import com.bumptech.glide.Glide |
||||
import com.lizongying.mytv.models.TVViewModel |
||||
|
||||
class CardPresenter( |
||||
private val owner: LifecycleOwner, |
||||
) : Presenter() { |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup): ViewHolder { |
||||
val cardView = object : |
||||
ImageCardView(ContextThemeWrapper(parent.context, R.style.CustomImageCardTheme)) {} |
||||
|
||||
cardView.isFocusable = true |
||||
cardView.isFocusableInTouchMode = true |
||||
return ViewHolder(cardView) |
||||
} |
||||
|
||||
override fun onBindViewHolder(viewHolder: ViewHolder, item: Any) { |
||||
val tvViewModel = item as TVViewModel |
||||
val cardView = viewHolder.view as ImageCardView |
||||
|
||||
cardView.titleText = tvViewModel.title.value |
||||
cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT) |
||||
cardView.tag = tvViewModel.videoUrl.value |
||||
|
||||
when (tvViewModel.title.value) { |
||||
"CCTV8K 超高清" -> Glide.with(viewHolder.view.context) |
||||
.load(R.drawable.cctv8k) |
||||
.centerInside() |
||||
.into(cardView.mainImageView) |
||||
|
||||
"天津卫视" -> Glide.with(viewHolder.view.context) |
||||
.load(R.drawable.tianjin) |
||||
.centerInside() |
||||
.into(cardView.mainImageView) |
||||
|
||||
"新疆卫视" -> Glide.with(viewHolder.view.context) |
||||
.load(R.drawable.xinjiang) |
||||
.centerInside() |
||||
.into(cardView.mainImageView) |
||||
|
||||
"兵团卫视" -> Glide.with(viewHolder.view.context) |
||||
.load(R.drawable.bingtuan) |
||||
.centerInside() |
||||
.into(cardView.mainImageView) |
||||
|
||||
else -> Glide.with(viewHolder.view.context) |
||||
.load(tvViewModel.logo.value) |
||||
.centerInside() |
||||
.into(cardView.mainImageView) |
||||
} |
||||
|
||||
cardView.setBackgroundColor(Color.WHITE) |
||||
cardView.setMainImageScaleType(ImageView.ScaleType.CENTER_INSIDE) |
||||
|
||||
tvViewModel.program.observe(owner) { _ -> |
||||
val program = tvViewModel.getProgramOne() |
||||
if (program != null) { |
||||
cardView.contentText = program.name |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun onUnbindViewHolder(viewHolder: ViewHolder) { |
||||
val cardView = viewHolder.view as ImageCardView |
||||
cardView.mainImage = null |
||||
} |
||||
|
||||
companion object { |
||||
private const val TAG = "CardPresenter" |
||||
private const val CARD_WIDTH = 300 |
||||
private const val CARD_HEIGHT = 101 |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@ |
||||
package com.lizongying.mytv |
||||
|
||||
import android.content.Context |
||||
import android.graphics.Canvas |
||||
import android.graphics.Paint |
||||
import android.graphics.Rect |
||||
import android.view.View |
||||
import androidx.core.content.ContextCompat |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
|
||||
class GrayOverlayItemDecoration(private val context: Context) : RecyclerView.ItemDecoration() { |
||||
|
||||
private val grayOverlayPaint = Paint().apply { |
||||
color = ContextCompat.getColor(context, R.color.gray_overlay) |
||||
style = Paint.Style.FILL |
||||
} |
||||
|
||||
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { |
||||
super.onDrawOver(c, parent, state) |
||||
|
||||
val childCount = parent.childCount |
||||
for (i in 0 until childCount) { |
||||
val child = parent.getChildAt(i) |
||||
if (!child.hasFocus()) { |
||||
// 计算遮罩层的大小 |
||||
val overlayRect = Rect( |
||||
child.left, |
||||
child.top, |
||||
child.right, |
||||
child.bottom |
||||
) |
||||
// 绘制灰色遮罩层 |
||||
c.drawRect(overlayRect, grayOverlayPaint) |
||||
} |
||||
} |
||||
} |
||||
|
||||
override fun getItemOffsets( |
||||
outRect: Rect, |
||||
view: View, |
||||
parent: RecyclerView, |
||||
state: RecyclerView.State |
||||
) { |
||||
super.getItemOffsets(outRect, view, parent, state) |
||||
// 在此处设置偏移量为0,以防止遮罩层影响项的布局 |
||||
outRect.setEmpty() |
||||
} |
||||
} |
||||
Loading…
Reference in new issue