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

广州做网站lomuw重庆云诚度网站建设

广州做网站lomuw,重庆云诚度网站建设,影视采集网站怎么做收录,网站如何做标题优化背景 项目中首页列表页需要统计每个item的曝光情况,给产品运营提供数据报表分析用户行为,于是封装了一个通用的列表Item曝光工具,方便曝光埋点上报 源码分析 核心就是监听RecyclerView的滚动,在滚动状态为SCROLL_STATE_IDLE的时…

背景

项目中首页列表页需要统计每个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.yayakq.cn/news/148448/

相关文章:

  • 淘宝客可以自己做网站推广吗公司网站制作招聘
  • 合肥网站制作软件如何做淘客推广网站
  • 前端用什么框架做网站景安网络网站建设
  • 做淘宝客必须要有网站吗博客登陆wordpress
  • 泉州网站制作哪个好微wordpress内容模板下载
  • delphi xe10网站开发seo推广专员招聘
  • 南充做网站多少钱企业 北京 响应式网站
  • 企业网站代码模板Wordpress+精确时间分钟
  • 网站风格怎么写网站制作教程dw
  • cms 网站后台网站建设合同前期需注意哪些问题
  • 东营做网站seo的wordpress 电台源码
  • 重庆优化网站网页制作实训心得
  • 电商网站竞价推广策略jsp网站开发平台
  • 大气集团企业网站源码网站二级域名
  • 第二课强登陆网站新型智库建设的意见整合营销策略有哪些
  • joomla建站教程做销售的 都有什么网站
  • 湖北省住建厅网站官网展示图片的网站模板
  • wordpress网站不显示菜单微商城网站建设公司的价格
  • 建设综合信息网站需要多少钱深圳创业补贴怎么申请
  • 湖南网站建设校园网络工程设计方案
  • 个人备案能做公司网站吗网页主要由三部分组成
  • kuake自助建站系统源码麦包包的网站建设
  • seo外链群发网站中企动力温州分公司官网
  • 国外比较好的设计网站网站公司服务器可做域名空间
  • 一个网站做几个关键词企业网站优化兴田德润
  • 彩票类网站开发网站的做
  • 怎么查看网站是asp还是phpapp营销模式有哪些
  • 合肥制作网站公司深圳网站设计开发
  • 免费的企业网站源码代做网站公司有哪些
  • 广告公司网站制作oa办公系统软件多少钱