当前位置: 首页 > news >正文

Android笔记(三十七):封装一个RecyclerView Item曝光工具——用于埋点上报

背景

项目中首页列表页需要统计每个item的曝光情况,给产品运营提供数据报表分析用户行为,于是封装了一个通用的列表Item曝光工具,方便曝光埋点上报

源码分析

  • 核心就是监听RecyclerView的滚动,在滚动状态为SCROLL_STATE_IDLE的时候开始计算哪些item是可见的
private fun calculateVisibleItemInternal() {if (!isRecording) {return}val lastRange = currVisibleRangeval currRange = findItemVisibleRange()val newVisibleItemPosList = createCurVisiblePosList(lastRange, currRange)visibleItemCheckTasks.forEach {it.updateVisibleRange(currRange)}if (newVisibleItemPosList.isNotEmpty()) {VisibleCheckTimerTask(newVisibleItemPosList, this, threshold).also {visibleItemCheckTasks.add(it)}.execute()}currVisibleRange = currRange}
  • 根据LayoutManager找出当前可见item的范围,剔除掉显示不到80%的item
private fun findItemVisibleRange(): IntRange {return when (val lm = currRecyclerView?.layoutManager) {is GridLayoutManager -> {val first = lm.findFirstVisibleItemPosition()val last = lm.findLastVisibleItemPosition()return fixCurRealVisibleRange(first, last)}is LinearLayoutManager -> {val first = lm.findFirstVisibleItemPosition()val last = lm.findLastVisibleItemPosition()return fixCurRealVisibleRange(first, last)}is StaggeredGridLayoutManager -> {val firstItems = IntArray(lm.spanCount)lm.findFirstVisibleItemPositions(firstItems)val lastItems = IntArray(lm.spanCount)lm.findLastVisibleItemPositions(lastItems)val first = when (RecyclerView.NO_POSITION) {firstItems[0] -> {firstItems[lm.spanCount - 1]}firstItems[lm.spanCount - 1] -> {firstItems[0]}else -> {min(firstItems[0], firstItems[lm.spanCount - 1])}}val last = when (RecyclerView.NO_POSITION) {lastItems[0] -> {lastItems[lm.spanCount - 1]}lastItems[lm.spanCount - 1] -> {lastItems[0]}else -> {max(lastItems[0], lastItems[lm.spanCount - 1])}}return fixCurRealVisibleRange(first, last)}else -> {IntRange.EMPTY}}}
  • 对可见的item进行分组,形成一个位置列表,上一次和这一次的分开组队
private fun createCurVisiblePosList(lastRange: IntRange, currRange: IntRange): List<Int> {val result = mutableListOf<Int>()currRange.forEach { pos ->if (pos !in lastRange) {result.add(pos)}}return result}
  • 将分组好的列表装进一个定时的task,在延迟一个阈值的时间后执行onVisibleCheck
class VisibleCheckTimerTask(input: List<Int>,private val callback: VisibleCheckCallback,private val delay: Long
) : Runnable {private val visibleList = mutableListOf<Int>()private var isExecuted = falseinit {visibleList.addAll(input)}fun updateVisibleRange(keyRange: IntRange) {val iterator = visibleList.iterator()while (iterator.hasNext()) {val entry = iterator.next()if (entry !in keyRange) {iterator.remove()}}}override fun run() {callback.onVisibleCheck( this, visibleList)}fun execute() {if (isExecuted) {return}mHandler.postDelayed(this, delay)isExecuted = true}fun cancel() {mHandler.removeCallbacks(this)}interface VisibleCheckCallback {fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: List<Int>)}
}
  • 达到阈值后,再获取一遍可见item的范围,对于仍然可见的item回调onItemShow
override fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: List<Int>) {val visibleRange = findItemVisibleRange()visibleList.forEach {if (it in visibleRange) {notifyItemShow(it)}}visibleItemCheckTasks.remove(task)}

完整源码

val mHandler = Handler(Looper.getMainLooper())class ListItemExposeUtil(private val threshold: Long = 100): RecyclerView.OnScrollListener(), VisibleCheckTimerTask.VisibleCheckCallback {private var currRecyclerView: RecyclerView? = nullprivate var isRecording = falseprivate var currVisibleRange: IntRange = IntRange.EMPTYprivate val visibleItemCheckTasks = mutableListOf<VisibleCheckTimerTask>()private val itemShowListeners = mutableListOf<OnItemShowListener>()fun attachTo(recyclerView: RecyclerView) {recyclerView.addOnScrollListener(this)currRecyclerView = recyclerView}fun start() {isRecording = truecurrRecyclerView?.post {calculateVisibleItemInternal()}}fun stop() {visibleItemCheckTasks.forEach {it.cancel()}visibleItemCheckTasks.clear()currVisibleRange = IntRange.EMPTYisRecording = false}fun detach() {if (isRecording) {stop()}itemShowListeners.clear()currRecyclerView?.removeOnScrollListener(this)currRecyclerView = null}override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {if (newState == RecyclerView.SCROLL_STATE_IDLE) {calculateVisibleItemInternal()}}private fun calculateVisibleItemInternal() {if (!isRecording) {return}val lastRange = currVisibleRangeval currRange = findItemVisibleRange()val newVisibleItemPosList = createCurVisiblePosList(lastRange, currRange)visibleItemCheckTasks.forEach {it.updateVisibleRange(currRange)}if (newVisibleItemPosList.isNotEmpty()) {VisibleCheckTimerTask(newVisibleItemPosList, this, threshold).also {visibleItemCheckTasks.add(it)}.execute()}currVisibleRange = currRange}private fun findItemVisibleRange(): IntRange {return when (val lm = currRecyclerView?.layoutManager) {is GridLayoutManager -> {val first = lm.findFirstVisibleItemPosition()val last = lm.findLastVisibleItemPosition()return fixCurRealVisibleRange(first, last)}is LinearLayoutManager -> {val first = lm.findFirstVisibleItemPosition()val last = lm.findLastVisibleItemPosition()return fixCurRealVisibleRange(first, last)}is StaggeredGridLayoutManager -> {val firstItems = IntArray(lm.spanCount)lm.findFirstVisibleItemPositions(firstItems)val lastItems = IntArray(lm.spanCount)lm.findLastVisibleItemPositions(lastItems)val first = when (RecyclerView.NO_POSITION) {firstItems[0] -> {firstItems[lm.spanCount - 1]}firstItems[lm.spanCount - 1] -> {firstItems[0]}else -> {min(firstItems[0], firstItems[lm.spanCount - 1])}}val last = when (RecyclerView.NO_POSITION) {lastItems[0] -> {lastItems[lm.spanCount - 1]}lastItems[lm.spanCount - 1] -> {lastItems[0]}else -> {max(lastItems[0], lastItems[lm.spanCount - 1])}}return fixCurRealVisibleRange(first, last)}else -> {IntRange.EMPTY}}}/*** 检查该item是否真实可见* view区域80%显示出来就算*/private fun checkItemCurrRealVisible(pos: Int): Boolean {val holder = currRecyclerView?.findViewHolderForAdapterPosition(pos)return if (holder == null) {false} else {val rect = Rect()holder.itemView.getGlobalVisibleRect(rect)if (holder.itemView.width > 0 && holder.itemView.height > 0) {return (rect.width() * rect.height() / (holder.itemView.width * holder.itemView.height).toFloat()) > 0.8f} else {return false}}}/*** 双指针寻找真实可见的item的范围(有一些item没显示完整剔除)*/private fun fixCurRealVisibleRange(first: Int, last: Int): IntRange {return if (first >= 0 && last >= 0) {var realFirst = firstwhile (!checkItemCurrRealVisible(realFirst) && realFirst <= last) {realFirst++}var realLast = lastwhile (!checkItemCurrRealVisible(realLast) && realLast >= realFirst) {realLast--}if (realFirst <= realLast) {realFirst..realLast} else {IntRange.EMPTY}} else {IntRange.EMPTY}}/*** 创建当前可见的item位置列表*/private fun createCurVisiblePosList(lastRange: IntRange, currRange: IntRange): List<Int> {val result = mutableListOf<Int>()currRange.forEach { pos ->if (pos !in lastRange) {result.add(pos)}}return result}/*** 达到阈值后,再获取一遍可见item的范围,对于仍然可见的item回调onItemShow*/override fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: List<Int>) {val visibleRange = findItemVisibleRange()visibleList.forEach {if (it in visibleRange) {notifyItemShow(it)}}visibleItemCheckTasks.remove(task)}private fun notifyItemShow(pos: Int) {itemShowListeners.forEach {it.onItemShow(pos)}}fun addItemShowListener(listener: OnItemShowListener) {itemShowListeners.add(listener)}
}interface OnItemShowListener {fun onItemShow(pos: Int)
}class VisibleCheckTimerTask(input: List<Int>,private val callback: VisibleCheckCallback,private val delay: Long) : Runnable {private val visibleList = mutableListOf<Int>()private var isExecuted = falseinit {visibleList.addAll(input)}fun updateVisibleRange(keyRange: IntRange) {val iterator = visibleList.iterator()while (iterator.hasNext()) {val entry = iterator.next()if (entry !in keyRange) {iterator.remove()}}}override fun run() {callback.onVisibleCheck(this, visibleList)}fun execute() {if (isExecuted) {return}mHandler.postDelayed(this, delay)isExecuted = true}fun cancel() {mHandler.removeCallbacks(this)}interface VisibleCheckCallback {fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: List<Int>)}
}
  • 测试代码
    在这里插入图片描述
  • 运行结果
    在这里插入图片描述
    在这里插入图片描述

http://www.mrgr.cn/news/76436.html

相关文章:

  • Linux探秘坊-------1.系统核心的低语:基础指令的奥秘解析(1)
  • 【AI大模型】大型语言模型LLM基础概览:技术原理、发展历程与未来展望
  • python——模块 迭代器 正则
  • 【含开题报告+文档+PPT+源码】基于Spring Boot智能综合交通出行管理平台的设计与实现
  • MacOS编译hello_xr——记一次CMake搜索路径限制导致的ANDROID_NATIVE_APP_GLUE not found
  • Day09 C++ 存储类
  • 微服务即时通讯系统的实现(客户端)----(1)
  • TCP连接秘籍:三次握手建立连接,四次挥手优雅告别
  • 8 软件项目管理
  • 狼蛛F87Pro键盘常用快捷键的使用说明
  • 麒麟kysec安全
  • 数据仓库面试题集离线实时
  • [JAVA]MyBatis环境配置介绍
  • 将已有的MySQL8.0单机架构变成主从复制架构
  • 【AI图像生成网站Golang】项目介绍
  • 2024数证杯电子取证比赛题目(初赛)
  • 1链式有序表的合并
  • 【提高篇】3.4 GPIO(四,工作模式详解 下)
  • [Linux]:Reactor模式
  • 牛客题库 21738 牛牛与数组
  • C语言数据结构与算法--简单实现队列的入队和出队
  • AVL树了解并简单实现
  • Linux网络编程
  • InternVL 多模态模型部署微调实践 | 书生大模型
  • 系统架构师考试18天极限备考复盘(2024年11月)
  • STM32芯片EXIT外部中断的配置与原理以及模板代码(标准库)