我有一个 ListView,它显示来自 ArrayAdapter 的项目。我想在单击时为视图设置动画。问题是在不同版本的android上我得到不同的视图(见下文)
我正在使用此方法从 ListActivity 获取视图:
private View getViewForListPosition(int position) {
int firstPosition = mList.getFirstVisiblePosition() - mList.getHeaderViewsCount();
int wantedChild = position - firstPosition;
ZLog.d(LOG,"getViewForListPosition , position : " + position + ", wantedChild : " + wantedChild + ", view hash : " +mList.getChildAt(wantedChild).hashCode());
for(int i = mList.getChildCount(); i>0; i--){
ZLog.d(LOG, "pos : " + (i-1) + ", hash : " +mList.getChildAt(i-1).hashCode());
}
return mList.getChildAt(wantedChild);
}
所以在 Android 4.0.3 和 4.2.2 手机上我得到:
getViewForListPosition , position : 3, wantedChild : 3, view hash : 1101734248
pos : 5, hash : 1104109360
pos : 4, hash : 1104254936
pos : 3, hash : 1101734248
pos : 2, hash : 1104876880
pos : 1, hash : 1104862296
pos : 0, hash : 1104793008
然后当单击项目时,我的适配器getView
方法为每个视图运行:
getView, position : 0, convertView is notnull, cv hash1104793008
getView, position : 1, convertView is notnull, cv hash1104862296
getView, position : 2, convertView is notnull, cv hash1104876880
getView, position : 3, convertView is notnull, cv hash1101734248
getView, position : 4, convertView is notnull, cv hash1104254936
getView, position : 5, convertView is notnull, cv hash1104109360
所以你可以看到一切都很好并且按预期工作。但是,当我在 Android 2.2 上运行它时,我得到的结果是:
getViewForListPosition , position : 3, wantedChild : 3, view hash : 1205607672
pos : 5, hash : 1205730120
pos : 4, hash : 1205712904
pos : 3, hash : 1205607672
pos : 2, hash : 1206547728
pos : 1, hash : 1206483960
pos : 0, hash : 1207864856
getView, position : 0, convertView is notnull, cv hash1205730120
getView, position : 1, convertView is notnull, cv hash1205712904
getView, position : 2, convertView is notnull, cv hash1205607672
getView, position : 3, convertView is notnull, cv hash1206547728
getView, position : 4, convertView is notnull, cv hash1206483960
getView, position : 5, convertView is notnull, cv hash1207864856
所以您可能已经注意到getViewForListPosition
,我会返回适配器用于位置 2 的视图
您可能还注意到Adapter.getView
或ListView.getChildAt
正在以相反的顺序返回项目,这会导致此问题。这种行为的原因可能是什么?(我没有在我的适配器中做任何花哨的事情)
我会感谢任何提示。谢谢!