android Recyclerview viewholder统一封装

ops/2025/1/18 13:35:34/

Recyclerview holder 统一封装 ViewHolder类

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.CountDownTimer
import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.tongtong.feat_watch.utils.WGlide/*** @author: shuhuai* @desc:* @date: 2024/11/15* @version:* @remark*/
open class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!
) {private var countDownTimer: CountDownTimer? = null//用于缓存已找的界面private val mView = SparseArray<View>()fun <T : View?> getView(viewId: Int): T? {//对已有的view做缓存var view = mView[viewId]//使用缓存的方式减少findViewById的次数if (view == null) {view = itemView.findViewById(viewId)mView.put(viewId, view)}return view as T?}fun startCountDown(viewId: Int,remainingTime: Long,copywrit: String? = "",unit: TimeUnit = TimeUnit.SECOND,backgroundColor: String = "00000000",callback: (message: String?) -> Unit = { }) {val view = getView<TextView>(viewId)!!countDownTimer = object : CountDownTimer(remainingTime, unit.v) {@RequiresApi(Build.VERSION_CODES.S)override fun onTick(millisUntilFinished: Long) {view.apply {text = TimeUnit.formatTime(millisUntilFinished, unit) + copywritval bg = background as GradientDrawableval code = String.format("#%s", backgroundColor)bg.setColor(Color.parseColor(code))}}override fun onFinish() {callback.invoke("倒计时结束")}}.start()}fun cancelCountDown() {if (countDownTimer != null) {countDownTimer!!.cancel()}}fun destoryDown() {cancelCountDown()countDownTimer = null}//通用的功能进行封装  设置文本 设置条目点击事件  设置图片fun setText(viewId: Int, text: CharSequence?): ViewHolder {val view = getView<TextView>(viewId)!!view.text = text//希望可以链式调用return this}//通用的功能进行封装  设置文本 设置条目点击事件  设置图片fun setText(viewId: Int, text: String?): ViewHolder {val view = getView<TextView>(viewId)!!view.text = text//希望可以链式调用return this}fun setTextColor(viewId: Int, color: Int): ViewHolder {val view = getView<TextView>(viewId)!!view.setTextColor(color)//希望可以链式调用return this}fun setTextBackground(viewId: Int, color: Int): ViewHolder {val view = getView<TextView>(viewId)!!view.setBackgroundColor(color)//希望可以链式调用return this}fun setTextTypeface(viewId: Int, style: Int): ViewHolder {val view = getView<TextView>(viewId)!!view.setTypeface(null, style)//希望可以链式调用return this}fun setSelected(viewId: Int, selected: Boolean): ViewHolder {val view = getView<TextView>(viewId)!!view.isSelected = selected//希望可以链式调用return this}fun setSelected2(viewId: Int, selected: Boolean): ViewHolder {val view = getView<View>(viewId)!!view.isSelected = selectedreturn this}fun setVisible(viewId: Int, visible: Boolean): ViewHolder {val view = getView<View>(viewId)!!view.visibility = if (visible) View.VISIBLE else View.GONEreturn this}fun setVisible(viewId: Int, visible: Boolean, isLocation: Boolean): ViewHolder {val view = getView<View>(viewId)!!if (isLocation) {view.visibility = if (visible) View.VISIBLE else View.INVISIBLE} else {view.visibility = if (visible) View.VISIBLE else View.GONE}return this}/*** 设置本地图片* @param viewId* @param resId* @return*/fun setImageResource(viewId: Int, resId: Int): ViewHolder {val iv = getView<ImageView>(viewId)!!iv.setImageResource(resId)return this}fun setDrawable(mContext: Context?, viewId: Int, resId: Int): ViewHolder {val v = getView<View>(viewId)!!v.setBackgroundDrawable(mContext?.resources?.getDrawable(resId))return this}fun setTextSelected(viewId: Int, bool: Boolean): ViewHolder {val tv = getView<TextView>(viewId)!!tv.isSelected = boolreturn this}/*** 设置本地图片* @param viewId* @param resId* @return*/fun setImageDrawable(mContext: Context, viewId: Int, resId: Int): ViewHolder {val iv = getView<ImageView>(viewId)!!iv.setImageDrawable(mContext.getResources().getDrawable(resId))return this}/*** 加载图片资源路径* @param viewId* @param imageLoader* @return*/fun setImagePath(viewId: Int, imageLoader: HolderImageLoader, res: Int): ViewHolder {val iv = getView<ImageView>(viewId)!!imageLoader.loadImage(iv, imageLoader.path, res)return this}fun setImageGrey(mContext: Context?, viewId: Int, url: String?): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImageGrey(mContext, view, url)return this}fun setImage(mContext: Context?, viewId: Int, url: String?): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImage(mContext, view, url)return this}fun setImage(mContext: Context?, viewId: Int, url: String?, res: Int): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImage(mContext, view, url)return this}fun setImage(mContext: Context?,viewId: Int,url: String?,width: Int?,height: Int?): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImage(mContext, view, url, width, height)return this}fun setImageRadius(mContext: Context?,viewId: Int,url: String?,radius: Float = 0f,): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImageRadius(mContext, view, url, radius)return this}fun setImageRadiusForTop(mContext: Context?,viewId: Int,url: String?,radiusTL: Float = 0f,radiusTR: Float = 0f,): ViewHolder {val view = getView<ImageView>(viewId)!!WGlide.setImageRadius(mContext, view, url, 0f, radiusTL, radiusTR)return this}@SuppressLint("NewApi")fun setBackground(mContext: Context, viewId: Int, bg: Int): ViewHolder {val tv = getView<View>(viewId) as TextViewtv.background = mContext.getResources().getDrawable(bg)return this}/*** 关于事件的*/fun setOnClickListener(viewId: Int, listener: View.OnClickListener?): ViewHolder {val view = getView<View>(viewId)!!view.setOnClickListener(listener)return this}abstract class HolderImageLoader(mContext: Context, var path: String) {var mContext: Context = mContext/*** 需要去复写这个方法加载图片* @param iv* @param path*/abstract fun loadImage(iv: ImageView?, path: String?, res: Int)}
}enum class TimeUnit(val v: Long) {DAY(24 * 60 * 60 * 1000),HOUR(60 * 60 * 1000),MINUTE(60 * 1000),HOUR_UN_SECOND(60 * 1000),HOUR_UN_SECOND_NO_ZERO(1000),SECOND(1000);companion object {fun formatTime(millis: Long, unit: TimeUnit): String {val totalSeconds: Long = millis / 1000val days: Long = totalSeconds / (24 * 3600) // 每天86400秒,24*3600val hours: Long = (totalSeconds % (24 * 3600)) / 3600 // 取余后计算小时val minutes: Long = (totalSeconds % 3600) / 60 // 取余后计算分钟val seconds: Long = totalSeconds % 60 // 取余后计算秒var time = "${days}${hours}${minutes}${seconds}秒"if (unit == HOUR) {time = "${(hours + days * 24)}${minutes}${seconds}秒"} else if (unit == MINUTE) {time = "${minutes + (hours + days * 24) * 60}${seconds}秒"} else if (unit == SECOND) {time = "${seconds + (minutes + (hours + days * 24) * 60) * 60}秒"} else if (unit == HOUR_UN_SECOND) {val _hour = hours + days * 24if (_hour > 0) {time = "${(hours + days * 24)}${minutes}分"} else {time = "${minutes}分"}} else if (unit == HOUR_UN_SECOND_NO_ZERO) {val _hour = hours + days * 24if (_hour > 0) {time = "${(hours + days * 24)}${minutes}分"} else {if (minutes > 0) {time = "${minutes}分"} else {time = "${seconds}秒"}}}return time;}}
}

具体实现

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.tongtong.feat_watch.R
import com.tongtong.feat_watch.ui.hall.bean.HallBean
import com.tongtong.feat_watch.ui.paly.view.scroll.ViewHolder/*** @author: shuhuai* @desc:* @date: 2025/1/15* @version:* @remark*/
class MyAdapter(context: Context?) :RecyclerView.Adapter<ViewHolder?>() {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = ViewHolder(LayoutInflater.from(parent?.context).inflate(R.layout.fragment_hall, parent, false))override fun onBindViewHolder(holder: ViewHolder, position: Int) {TODO("Not yet implemented")}override fun getItemCount(): Int {TODO("Not yet implemented")}
}

http://www.ppmy.cn/ops/151101.html

相关文章

【大数据】机器学习-----------贝叶斯分类器

1. 贝叶斯决策论 贝叶斯决策论的核心是基于贝叶斯定理来计算后验概率 (P(c|x))&#xff0c;根据贝叶斯定理&#xff1a; 其中&#xff1a; (P(c|x)) 是后验概率&#xff0c;即在特征 (x) 出现的情况下&#xff0c;样本属于类别 (c) 的概率。(P(x|c)) 是类条件概率&#xff0c…

某国际大型超市电商销售数据分析和可视化

完整源码项目包获取→点击文章末尾名片&#xff01; 本作品将从人、货、场三个维度&#xff0c;即客户维度、产品维度、区域维度&#xff08;补充时间维度与其他维度&#xff09;对某国际大型超市的销售情况进行数据分析和可视化报告展示&#xff0c;从而为该超市在弄清用户消费…

ubuntu的截图工具有哪些

在 Ubuntu 上&#xff0c;有多种截图工具可供选择&#xff0c;以下是一些常用的工具及其使用方法&#xff1a; 1. 内置截图工具 Ubuntu 自带了一个简单的截图工具&#xff0c;可以通过快捷键或应用程序启动。 快捷键&#xff1a; 全屏截图&#xff1a;Print Screen&#xff08…

使用FineBI进行数据分析(入门级)

使用FineBI进行数据分析&#xff08;入门级&#xff09; 实验背景 哔哩哔哩&#xff0c;简称 B 站&#xff0c;现为中国年轻世代高度聚集的文化社区和视频平台&#xff0c;该网站于 2009 年 6 月 26 日创建&#xff0c; 2018 年 3 月 28 日在美国纳斯达克上市。早期 B 站是一…

CCLINKIE转ModbusTCP网关,助机器人“掀起”工业智能的“惊涛骇浪”

以下是一个稳联技术CCLINKIE转ModbusTCP网关&#xff08;WL-CCL-MTCP&#xff09;连接三菱PLC与机器人的配置案例&#xff1a;设备与软件准备设备&#xff1a;稳联技术WL-CCL-MTCP网关、三菱FX5UPLC、支持ModbusTCP协议的机器人、网线等。 稳联技术ModbusTCP转CCLINKIE网关&…

C++并发编程之异常安全性增强

在并发编程中&#xff0c;异常安全是一个非常重要的方面&#xff0c;因为并发环境下的错误处理比单线程环境更加复杂。当多个线程同时执行时&#xff0c;异常不仅可能影响当前线程&#xff0c;还可能影响其他线程和整个程序的稳定性。以下是一些增强并发程序异常安全性的方法&a…

Kafka 控制生产者流量

在 Apache Kafka 中&#xff0c;控制生产者流量&#xff08;即控制生产者的发送速率和负载&#xff09;通常涉及以下几个方面&#xff1a;流量控制、速率限制、缓冲区管理等。Kafka 生产者有一些内置配置&#xff0c;可以帮助我们限制数据的生产速率&#xff0c;从而防止过高的…

C++ QT 自绘呼吸灯

功能 使用QLabel生成一个呼吸灯的效果&#xff0c;用于显示某个状态的变化h #ifndef CUELIGHTLABEL_H #define CUELIGHTLABEL_H#include <QLabel> #include <QPropertyAnimation>class CueLightLabel : public QLabel {Q_OBJECTQ_PROPERTY(QColor color READ get…