5

我有一个 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);
    }
4

1 回答 1

1

该问题的解决方案解释了了解 ListView childAt 方法以及限制 listview.getChildAt(index) 索引的因素listView.getChildCount()问题是我误解了和之间的关系adapter.getCount()

简单来说:

根据我的实验,这是它的工作原理。ListView 是适配器的一部分。因此,如果一个适配器有 500 个项目,而 ListView 有十 (10) 个项目。ListView 中的十个代表动态视图。因此,如果 firstVisibleItem 是适配器中的项目 217,则 ListView 的索引范围将为 (217,…,226),而 listView.getChildCount() 仍将返回 10。

因此答案是:

getChildAt(x) | x : [0+firstVisibleItem, listview.getChildCount()+firstVisibleItem)
于 2013-10-03T19:39:08.357 回答