越来越查看关于2.2 ListView项/相反的顺序;适用于4.0.3 [英] getting View for ListView item / reverse order on 2.2; works on 4.0.3

查看:206
本文介绍了越来越查看关于2.2 ListView项/相反的顺序;适用于4.0.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,显示从ArrayAdapter项目。我想,当它被cliked动画视图。 问题是,在不同版本的Andr​​oid我得到不同的看法(见下文)

I have a ListView which shows items from ArrayAdapter. I want to animate the view when it is cliked. The problem is that on different versions of android I am getting different views (see below)

我是从ListActivity用这种方法得到的观点:

I'm getting the view from ListActivity using this method :

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手机获取:

So that on Android 4.0.3 & 4.2.2 phone I get :

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 运行方式为每个视图:

then when item is clicked 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

所以你可以看到一切都很好,工作正常。 然而,当我运行这个在Android 2.2这些结果我得到:

so you can see everything is fine and works as expected. However when I run this on Android 2.2 these are the results I am getting :

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位认为

so as you might have noticed getViewForListPosition will give me back the view which adapter uses for position 2

您可能也注意到,无论是 Adapter.getView ListView.getChildAt 将返回按相反的顺序项目导致这个问题。有什么能为这种现象的原因是什么? (我没有做任何幻想在我的适配器)

You might have also noticed that either the Adapter.getView or ListView.getChildAt is returning items in reverse order which causes this issue. What could be the reason for this behaviour? (I'm not doing anything fancy in my adapter)

我会感谢任何暗示。谢谢!

I will be thankful for any kind of hint. Thanks!

推荐答案

确定。所以这是发生了什么:

OK. so this is what is happening :

在一个干净的的ListView 当我注册 onItemClickListener 并进行点击适配器 getView 法的意见不被调用。这是我所期望的。

On a clean ListView When I register onItemClickListener and perform clicks adapter getView method for views is not called. This is what I would expect.

如果我设置了 mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE)这使得 getView 方法运行后点击右边是注册。这也符合预期。

If I set a mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE) this makes getView methods run right after click is registered. This is also what is expected.

然而2.2和4+版本之间的区别在于: 在2.2:在此刷新发生 getView 运行在各种观点的反转列表(见我的问题) 在4+(可能API 11+): getView 运行列表正常秩序

However the difference between 2.2 and 4+ versions is that : on 2.2 : when this refresh happens getView operates on a reversed list of views (see my question) on 4+ (possibly API 11+) : getView operates on normal order of list

什么是最有趣的部分是,当我耽误调用getViewForListPosition由10ms的一切工作正常,适配器的意见正确列表(再正常秩序)。如此看来,意见订单仅在适配器的刷新与 CHOICE_MODE_SINGLE

What is the most interesting part is that when I delay call to getViewForListPosition by 10ms everything works fine and adapter has proper list of views (normal order again). So it seems that the order of views is reversed only during adapter refresh with CHOICE_MODE_SINGLE

要解决这个问题,我不更改列表视图模式 CHOICE_MODE_SINGLE 使适配器不点击时触发。我设置BG /图形,点击项目在我自己的里面 onItemClicked

To solve this problem I dont change listview mode to CHOICE_MODE_SINGLE so that adapter doesn't fire during click. I set bg/graphics for clicked item on my own inside onItemClicked

希望这样可以节省别人一些时间:)

hope that it saves someone some time :)

这篇关于越来越查看关于2.2 ListView项/相反的顺序;适用于4.0.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆