Cot*_*nyo 5 android scroll coordinates android-layout android-listview
我有一个ListView与许多项目(动物),具有不同的高度.使用OnScrollListener我试图跟踪哪个项目与屏幕上的特定位置相交.说有问题的位置是creatureMarkerBottom = 140.当我运行代码时,下面的代码似乎返回了错误的数据:我不断得到误报和漏报.这是代码.该代码应该使标记变为实心或透明,这取决于鸡是否与它交叉.然而,褪色并不能真正服从鸡是否接触到板/棒.我的猜测是我获取ListView像素位置的方式是错误的.
OnScrollListener listviewScrollListener = new OnScrollListener() {
int creatureLocationPixel[] = { 0, 0 };
int creatureMarkerBottom;
int creatureTop, creatureBottom;
int[] creatureLocationPixel = { 0, 0 };
View creatureView;
boolean creatureMarkerIsFaded = false;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
try {
scrollBackgroundToFindCreature(visibleItemCount, firstVisibleItem);
} catch (Exception e) {
e.printStackTrace();
}
}
private void scrollBackgroundToFindCreature(int visibleItemCount, int index) {
creatureMarkerSlabView.getLocationOnScreen(creatureLocationPixel);
creatureMarkerBottom = creatureLocationPixel[1] + creatureMarkerSlabView.getHeight();
Animal creature;
boolean found = false;
do {
creature = mAdapter.getItem(index);
creatureView = getViewForPosition(index);
creatureView.getLocationOnScreen(creatureLocationPixel);
creatureTop = creatureLocationPixel[1];
creatureBottom = creatureTop + creatureView.getHeight();
if (creatureTop < creatureMarkerBottom && creatureMarkerBottom < creatureBottom) {
found = true;
} else {
index++;
}
} while (!found && index < visibleItemCount);
if (creatureType.CHICKEN != creature.getType()) {
if (!creatureMarkerIsFaded) {
creatureMarkerIsFaded = true;
for (int x = 0; x < creatureMarkerSlabView.getChildCount(); x++)
creatureMarkerSlabView.getChildAt(x).setAlpha(TRANSPARENCY_ALPHA);
creatureMarkerSlabView.setAlpha(TRANSPARENCY_ALPHA);
}
} else {
if (creatureMarkerIsFaded) {
creatureMarkerIsFaded = false;
for (int x = 0; x < creatureMarkerSlabView.getChildCount(); x++)
creatureMarkerSlabView.getChildAt(x).setAlpha(255);
creatureMarkerSlabView.setAlpha(255);
}
}
}
};
public View getViewForPosition(int position) {
int firstPosition = animalListview.getFirstVisiblePosition() - animalListview.getHeaderViewsCount();
int wantedChild = position - firstPosition;
// Say, first visible position is 8, you want position 10, wantedChild will now be 2
// So that means your view is child #2 in the ViewGroup:
if (wantedChild < 0 || wantedChild >= animalListview.getChildCount()) {
return null;
}
return animalListview.getChildAt(wantedChild);
}
Run Code Online (Sandbox Code Playgroud)
问题的解决方案是通过了解 ListView childAt 方法以及限制 listview.getChildAt(index) 索引的内容来解释的。问题是我误解了listView.getChildCount()和之间的关系adapter.getCount()。
简单来说:
\n\n根据我的实验,它是这样工作的。ListView 是适配器的一部分。因此,如果适配器有 500 个项目,而 ListView 有十 (10) 个项目。ListView中的十个代表一个动态视图。因此,如果firstVisibleItem是适配器中的项目217,则ListView索引范围将为(217,\xe2\x80\xa6,226),而listView.getChildCount()仍将返回10。
\n\n因此答案是:
\n\ngetChildAt(x) | x : [0+firstVisibleItem, listview.getChildCount()+firstVisibleItem)\nRun Code Online (Sandbox Code Playgroud)\n