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

网优 是什么网站网站代码的重点内容是什么

网优 是什么网站,网站代码的重点内容是什么,贵阳市城乡建设局网站,wordpress购买资源插件背景 项目中首页列表页需要统计每个item的曝光情况#xff0c;给产品运营提供数据报表分析用户行为#xff0c;于是封装了一个通用的列表Item曝光工具#xff0c;方便曝光埋点上报 源码分析 核心就是监听RecyclerView的滚动#xff0c;在滚动状态为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): ListInt {val result mutableListOfInt()currRange.forEach { pos -if (pos !in lastRange) {result.add(pos)}}return result}将分组好的列表装进一个定时的task在延迟一个阈值的时间后执行onVisibleCheck class VisibleCheckTimerTask(input: ListInt,private val callback: VisibleCheckCallback,private val delay: Long ) : Runnable {private val visibleList mutableListOfInt()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: ListInt)} }达到阈值后再获取一遍可见item的范围对于仍然可见的item回调onItemShow override fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: ListInt) {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 mutableListOfVisibleCheckTimerTask()private val itemShowListeners mutableListOfOnItemShowListener()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): ListInt {val result mutableListOfInt()currRange.forEach { pos -if (pos !in lastRange) {result.add(pos)}}return result}/*** 达到阈值后再获取一遍可见item的范围对于仍然可见的item回调onItemShow*/override fun onVisibleCheck(task: VisibleCheckTimerTask, visibleList: ListInt) {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: ListInt,private val callback: VisibleCheckCallback,private val delay: Long) : Runnable {private val visibleList mutableListOfInt()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: ListInt)} }测试代码 运行结果
http://www.dnsts.com.cn/news/78783.html

相关文章:

  • 临沂品牌网站建设公司网站多个页面要加引导
  • 天津电子商务网站怎样学做网站
  • 网站信息备案变更 哪里做如何申请建设网站首页
  • 高端网站设计报价表wanwang
  • 大学学校网站建设方案简单小网站
  • 黑帽seo怎么做网站排名制作网页的图片
  • 网站简单设计网站两侧对联广告图片
  • 河南平顶山网站建设公司南昌百度推广公司
  • 南雄市建设局网站博物馆建设网站
  • 尤溪建设局网站建设银行北海分行网站
  • 三网合一 营销型网站有什么好的网站可以接单子做
  • 天王手表官方网站免费域名注册2023
  • 广州品牌网站china东莞seo
  • 南山建设网站现在最新技术有哪些
  • 如何利用源码做网站wordpress 画图插件
  • 建筑认证四川网站seo
  • 怎么做点播网站wordpress页面设置方法
  • 天津免费做网站山东seo优化
  • 做网站参考文献男女做暧昧视频网站
  • 公司搭建网站昆山网站建设昆山
  • 珠海网站建设技术外包专业做域名的网站
  • 哈尔滨网站基础优化公众号1000粉丝月收入
  • 不良网站举报中心官网在相亲网站做红娘
  • 山西营销网站建设设计济南专业网站建设咨询
  • 南京机关建设网站桂林做网站公司
  • 一个主机可以做几个网站出入成都最新规定今天
  • 礼品网站建设wordpress常见插件
  • 物流系统网站策划书怎样做艾条艾柱网站
  • 下载学校网站模板下载网站备份文件
  • 网站宣传与推广的方法WordPress如何清空评论