HarmonyOS 鸿蒙Next加载图片列表时超过120项出现卡顿
HarmonyOS 鸿蒙Next加载图片列表时超过120项出现卡顿
代码如下:
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.request.RequestOptions
import com.lyxc.live.R
import com.lyxc.live.commonUtils.Res
import com.lyxc.live.commonUtils.ValidateUtils
import com.lyxc.live.databinding.ItemNewLiveBinding
import com.lyxc.live.glide.GlideApp
import com.lyxc.live.ui.live.match.MatchLiveItem
import com.lyxc.live.ui.mine.ztools.GlideRoundTransformCenterCrop
import java.text.SimpleDateFormat
class NewLiveAdapter(val action: (MatchLiveItem) -> Unit) : RecyclerView.Adapter<NewLiveAdapter.ViewHolder>() {
private val dataList = arrayListOf<MatchLiveItem>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemNewLiveBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//点击事件
val item = dataList[position]
holder.binding.root.setOnClickListener {
action(item)
}
holder.bind(item)
}
class ViewHolder(val binding: ItemNewLiveBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: MatchLiveItem) {
binding.tvName.text = item.name
GlideApp.with(binding.root).load(ValidateUtils.setXYPic(item.cover, ValidateUtils.COURSE_LAR))
.apply(RequestOptions.bitmapTransform(GlideRoundTransformCenterCrop(binding.root.context, Res.getDimen(R.dimen.x5).toInt())))
.into(binding.im)
when (item.status) {
0 -> {
binding.tvNumber.visibility = View.GONE
binding.tvTime.visibility = View.VISIBLE
binding.llNow.visibility = View.GONE
binding.tvTime.text = getTime(item.openTime)
}
1 -> {
GlideApp.with(binding.root).asGif().load(R.drawable.ic_live).into(binding.icLive)
binding.tvTime.visibility = View.GONE
binding.llNow.visibility = View.VISIBLE
if (item.isSeeWatchNum == 1) {
binding.tvNumber.visibility = View.VISIBLE
binding.tvNumber.text = item.getFormatCount() + "人观看"
} else {
binding.tvNumber.visibility = View.GONE
}
binding.llNowOrage.visibility = View.VISIBLE
binding.tvRe.visibility = View.GONE
}
2 -> {
GlideApp.with(binding.root).asGif().load(R.drawable.ic_live).into(binding.icLive)
binding.tvTime.visibility = View.GONE
binding.llNow.visibility = View.VISIBLE
if (item.isSeeWatchNum == 1) {
binding.tvNumber.visibility = View.VISIBLE
binding.tvNumber.text = item.getFormatCount() + "人观看"
} else {
binding.tvNumber.visibility = View.GONE
}
binding.llNowOrage.visibility = View.VISIBLE
binding.tvRe.visibility = View.GONE
}
3 -> {
if (item.isSeeWatchNum == 1) {
binding.tvNumber.visibility = View.VISIBLE
if (TextUtils.isEmpty(item.startTime)) {
binding.tvNumber.text = item.getFormatHistoryCount() + "人观看"
} else {
binding.tvNumber.text = item.getFormatHistoryCount() + "人观看 | " + item.startTime.substring(0, 10)
}
} else {
if (TextUtils.isEmpty(item.startTime)) {
binding.tvNumber.visibility = View.GONE
} else {
binding.tvNumber.visibility = View.VISIBLE
binding.tvNumber.text = item.startTime.substring(0, 10)
}
}
binding.tvNumber.visibility = View.VISIBLE
binding.tvTime.visibility = View.GONE
binding.llNow.visibility = View.VISIBLE
binding.llNowOrage.visibility = View.GONE
binding.tvRe.visibility = View.VISIBLE
}
4 -> {
binding.tvNumber.visibility = View.GONE
binding.tvTime.visibility = View.GONE
binding.llNow.visibility = View.GONE
}
}
binding.executePendingBindings()
}
fun getTime(time: String): String {
var sdf1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
var date = sdf1.parse(time)
var sdf2 = SimpleDateFormat("M月d日 HH:mm开播")
return sdf2.format(date)
}
}
//数据变动
fun dataChange(newData: List<MatchLiveItem>) {
dataList.clear()
dataList.addAll(newData)
notifyDataSetChanged()
}
}
加载多了就特别的卡顿
更多关于HarmonyOS 鸿蒙Next加载图片列表时超过120项出现卡顿的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
华为官网https://developer.huawei.com/consumer/cn/doc/best-practices-V5/bpta-best-practices-long-list-V5 动态预加载给出了解决方案,但是我看代码感觉不能很清楚的与我结合起来处理加载图片长列表的问题
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
您好,这边还麻烦您通过在线提单进一步解决:https://developer.huawei.com/consumer/cn/support/feedback/#/,如果还有其他问题,也欢迎再次发帖咨询,谢谢!
欢迎开发小伙伴们进来帮帮楼主
在HarmonyOS鸿蒙系统中,当加载图片列表超过120项时出现卡顿的问题,通常与资源管理和UI渲染效率有关。以下是可能的原因及解决方法概述:
-
内存与资源管理:大量图片加载会占用大量内存,导致系统资源紧张。建议采用图片懒加载技术,即只加载用户当前可见或即将可见的图片,减少内存占用。同时,可以使用图片压缩技术降低图片质量,从而减少内存占用。
-
UI渲染效率:当图片数量过多时,UI渲染负担加重。可以考虑使用分页加载或虚拟列表技术,只渲染当前可见区域的图片,减少渲染次数。此外,优化布局算法和减少不必要的重绘也能提升渲染效率。
-
异步处理:将图片加载和处理任务放在后台线程或异步任务中执行,避免阻塞主线程,从而保持UI的流畅性。
-
硬件加速:确保应用已启用硬件加速,以利用GPU进行图像渲染,提高渲染效率。
如果以上方法无法完全解决卡顿问题,可能是由于系统或应用层面的更深层次原因。此时,建议深入分析应用的性能瓶颈,或考虑对图片加载和渲染机制进行更全面的优化。如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html 。