Android – get listview items / views in reverse order on 2.2; Applicable to 4.0.3

I have a listview that displays items in the arrayadapter. I want to animate the view when it is blocked. The problem is that I get different views on different versions of Android (see below)

I am using this method to get views from 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);
}

Invalid Sign

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

Then, when I click item, my adapter getview method runs for each view:

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

So you can see that everything is fine and works as expected. But when I run it on Android 2.2, these are the results I get:

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

Therefore, you may have noticed that getviewforlistposition will return the view used by the adapter for location 2

You may also notice that adapter.getview or listview.getchildat returns items in reverse order, which will cause this problem. What are the possible reasons for this behavior? (I didn't do anything strange in my adapter)

I will appreciate any hint. Thank you!

resolvent:

OK. So that's what happened:

On a clean listview, when I register onitemclicklistener and execute the click adapter, the getview method of the view will not be called. That's what I expect

If I set an MLIST. Setchoicemode (listview. Choice_mode_single), this will make the getview method run immediately after clicking. This is also expected

However, the difference between versions 2.2 and 4 is: on version 2.2: when this refresh occurs, getview runs on an inverted view list (see my question) 4 (possibly API 11): getview runs in the normal list order

The most interesting part is that when I delay calling getviewforlistposition for 10ms, everything is normal and the adapter has an appropriate view list (normal order again). Therefore, only when using choice_ MODE_ Single reverses the order of views only when the adapter is refreshed

To fix this problem, I will not change the listview mode to choose_ MODE_ Single so that the adapter will not be triggered during click. I set BG / graphics for my own click items on onitemclicked

Hope to save some time:)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>