在Android ListView中跟踪项目的印象 [英] Track impression of items in an android ListView

查看:109
本文介绍了在Android ListView中跟踪项目的印象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView 和一个 ArrayAdapter< CustomListViewItem> 设置为其适配器.我想跟踪每个 CustomListViewItem 项目的印象数.

I have a ListView and an ArrayAdapter < CustomListViewItem > set as its adapter. I would like to track number of impressions of each CustomListViewItem item.

只要有一个ListView项目进入视野,就应该算作一次展示.当它消失,然后再次出现时,印象数就会增加.当用户滚动时,某些项目将不在视图中,而另一些将进入视图.我想跟踪同时可见和不可见的项目.

Whenever a ListView item comes into view that should count as one impression. When it goes out of view and then again comes into view the impression count increments. When user scrolls, some items will go out of the view and some will come into view. I want to track the items which both come into view and move out of view.

我尝试过的事情:

我尝试的一件事是 setOnScrollListener 作为列表 查看并在其中进行跟踪 onScroll(AbsListView视图,int firstVisibleItem,int visibleItemCount,int totalItemCount)回调.

One thing i tried was to setOnScrollListener for the list view and do tracking in the onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) callback.

我可以使用 firstVisibleItem visibleItemCount 来完成这项工作. 在文档中,它说滚动完成后会调用它.但是,当滚动缓慢时,它似乎已被调用很多次.可能在每个滚动像素之后.这使滚动变得非常缓慢.

I can use firstVisibleItem and visibleItemCount to do the job. In the documentation it says that this is called when the scroll has completed. But it seems to be getting called a lot of times when the scrolling is slow. Probably after each pixel of scroll. This is making the scroll very laggy.

还有其他方法可以做到吗?通过使用某种回调,当每个项目不在视图中或进入视图时都会被调用?

Is there any other way to do this? By using some kind of callback which gets called for each item when it goes out of view or comes into view?

我在网上搜索了很多内容,却没有发现与此类跟踪相关的任何帖子.

I searched a lot on web and didn't see any posts related to such kind of tracking.

推荐答案

我找不到另一种方法.因此,我优化了我已经在做的事情.我认为我应该分享这一点,因为它可能会对其他人有所帮助.

I couldn't find another way to do this. So, i optimized what i was already doing. I thought i should share this as it may help someone else.

我使用 onScroll() firstVisibleItem visibleItemCount 仅回调.我使用两个变量来保留先前onScroll()回调的信息.他们是:

I use firstVisibleItem and visibleItemCount of onScroll() callback only. I use two variables to keep information of previous onScroll() callback. They are :

prevVisibleStart prevVisibleEnd

让我们举个例子来了解算法. 最初假设在第一次onScroll()

Lets take an example to understand the algorithm. Assume initially that after first onScroll()

f irstVisibleItem = 0 visibleItemCount = 3 => lastVisibleItem = 2

这些用于设置我以前的可见状态.

These are used to set my previous visible states.

因此, prevVisibleStart = firstVisibleItem = 3 prevVisibleEnd = firstVisibleItem + visibleItemCount-1 = 2

现在,如果我向下滚动,我将一直拥有

Now if i scroll down, i will always have

firstVisibleItem > = prevVisibleStart lastVisibleItem > = prevVisibleEnd

firstVisibleItem >= prevVisibleStart and lastVisibleItem >= prevVisibleEnd

因此,如果先前的范围是0到2.则新的范围将有点像1到3.因为,我已经考虑过印象中的0到2.我只会将3视为新印象.原因是视图1、2仍在视图中,并且它们并未消失.因此,不为这些增加新的印象.这样不仅可以有效地确定要考虑的展示次数范围,还可以减少检查范围.

So, if the previous range was 0 to 2. The new range would be somewhat like 1 to 3. Since, i have already considered 0 to 2 for impression. I will only consider 3 as new impression. The reason being the views 1, 2 are still in view and they hadn't gone out of view. So, a new impression is not counted for these. This effectively gives the correct range to consider for impressions as well as reduces the range for checking as well.

类似地,可以完成此操作以向上滚动. 假设当前可见项目为2-4. 现在,在增加范围之后,范围变为(例如)1-3.这意味着已经出现了1.因此,请为该视图计算一次印象.这样,onScroll()中的操作数将大大减少.

Similarly, this can be done for scrolling up. Assume that the current visible items are 2-4. Now after scroling up the range becomes (say)1-3. This means that 1 has come into view. So, count an impression for this view. This way the number of operations in onScroll() significantly reduce.

紧随其后的示例代码

范围[i-j)被认为是印象.

range [i-j) is considered for impression.

private static int prevVisibleStart=-1;
private static int prevVisibleEnd=-1;
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
 if(prevVisibleStart <= firstVisibleItem) {
  // scroll down
  i = Math.max(firstVisibleItem, prevVisibleEnd + 1);
  j = firstVisibleItem + visibleItemCount;
 } else {
  // scroll up
  i = firstVisibleItem;
  j = Math.min(prevVisibleStart, firstVisibleItem + visibleItemCount);
 }
 prevVisibleStart = firstVisibleItem;
 prevVisibleEnd = firstVisibleItem + visibleItemCount - 1;
 // now can use [i-j) to update impressions
}

这篇关于在Android ListView中跟踪项目的印象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆