4

ArrayAdapter の項目を表示する ListView があります。クリックされたときにビューをアニメーション化したい。問題は、さまざまなバージョンの 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が逆の順序でアイテムを返していることに気付いたかもしれません。これがこの問題の原因です。この動作の理由は何でしょうか? (私はアダプターで何も派手なことをしていません)

あらゆる種類のヒントに感謝します。ありがとう!

4

1 に答える 1

3

わかった。これが起こっていることです:

きれいListViewに 登録してクリックを実行すると、ビューのonItemClickListenerアダプターメソッドが呼び出されません。getViewこれは私が期待するものです。

mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE)これを設定するとgetView、クリックが登録された直後にメソッドが実行されます。これも想定内です。

ただし、2.2 バージョンと 4+ バージョンの違いは次のとおりです: 2.2 の場合: この更新が発生getViewすると、ビューの逆のリストで動作します (私の質問を参照) 4+ (おそらく API 11+):getViewリストの通常の順序で動作します

最も興味深い部分は、getViewForListPosition への呼び出しを 10 ミリ秒遅らせると、すべてが正常に機能し、アダプターにビューの適切なリスト (再び通常の順序) があることです。したがって、ビューの順序が逆になるのは、アダプタのリフレッシュ中のみであると思われますCHOICE_MODE_SINGLE

この問題を解決するためCHOICE_MODE_SINGLEに、クリック中にアダプターが起動しないようにリストビューモードを変更しません。クリックされたアイテムの背景/グラフィックを自分で設定しましたonItemClicked

誰かの時間を節約できることを願っています:)

于 2013-04-04T11:54:32.240 に答える