getChildAt返回一个null的视图

时间:2013-01-08 10:42:36

标签: android

我尝试在ListView中设置单个项目的动画,我发现这有点难。但是,经过一些谷歌搜索,我想出了一些我合并的代码片段,但是当我尝试调用我的ListView的{​​{1}}以返回视图时,返回null。

getChildAt

来自代码:

public void animateSingleItemInListView() { final Animation animBounce = AnimationUtils.loadAnimation(context, R.anim.bounce); int totalItemsInListView = lastCases.getCount(); int wantedPosition = 1; // Whatever position you're looking for int firstPosition = lastCases.getFirstVisiblePosition() - lastCases.getHeaderViewsCount(); // This is the same as child #0 int wantedChild = wantedPosition - 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: int childs = lastCases.getChildCount(); if (wantedChild < 0) { Log.w("UPDATEUI", "Unable to get view for desired position, because it's not being displayed on screen."); return; } // Could also check if wantedPosition is between listView.getFirstVisiblePosition() and listView.getLastVisiblePosition() instead. View wantedView = lastCases.getChildAt(firstPosition); wantedView.setAnimation(animBounce); } 返回10,这是我totalItemsInListView中的正确行数。整数ListView当然是0,但firstPosition方法只返回n​​ull。怎么会?如何从getChildAt()获取第一个View

1 个答案:

答案 0 :(得分:-1)

尝试使用

lastCases.getItemAtPosition(firstPosition)