IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

ExpandableListViewのPackedPosition

よくわからなかったのでコードを書いてログを出して確認してみました。

PackedPositionは内部的に管理されているポジションのようです。 PackedPositionはタイプがあって、親用(グループ)と子供用があります(Nullというのもあるが)。

使う時のパターンとしては

  1. 見えているアイテムの位置を取得
  2. PackedPositionの取得
  3. 親用(グループ)の位置の取得
  4. 子供用の位置の取得

となると思います。

  • PackedPositionのタイプが子供の時は、そのPackedPositionから親用(グループ)の位置の取得と子供用の位置の取得の両方が取得できます。
  • PackedPositionのタイプが親用(グループ)の時は、そのPackedPositionから親用(グループ)の位置の取得ができ、子供用の位置は-1となります。

スクロールした時にログを出すコード

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        ExpandableListView exp = (ExpandableListView) view;
        long getExpandableListPosition = exp.getExpandableListPosition(firstVisibleItem);
        int getPackedPositionType = ExpandableListView.getPackedPositionType(getExpandableListPosition);
        int getPackedPositionGroup = ExpandableListView.getPackedPositionGroup(getExpandableListPosition);
        int getPackedPositionChild = ExpandableListView.getPackedPositionChild(getExpandableListPosition);

        Log.d("xxx", String.format(
                "1st=%d, packec=%d, type=%d group=%d, child=%d",
                firstVisibleItem,
                getExpandableListPosition,
                getPackedPositionType,
                getPackedPositionGroup,
                getPackedPositionChild

        ));
    }

親のログ

/com.example.test11 D/xxx: 1st=3, packec=12884901888, type=0 group=3, child=-1
/com.example.test11 D/xxx: 1st=3, packec=12884901888, type=0 group=3, child=-1
/com.example.test11 D/xxx: 1st=3, packec=12884901888, type=0 group=3, child=-1
/com.example.test11 D/xxx: 1st=4, packec=17179869184, type=0 group=4, child=-1

子供のログ

/com.example.test11 D/xxx: 1st=5, packec=-9223372019674906624, type=1 group=4, child=0
/com.example.test11 D/xxx: 1st=5, packec=-9223372019674906624, type=1 group=4, child=0
/com.example.test11 D/xxx: 1st=5, packec=-9223372019674906624, type=1 group=4, child=0
/com.example.test11 D/xxx: 1st=5, packec=-9223372019674906624, type=1 group=4, child=0
/com.example.test11 D/xxx: 1st=6, packec=-9223372019674906623, type=1 group=4, child=1
/com.example.test11 D/xxx: 1st=6, packec=-9223372019674906623, type=1 group=4, child=1
/com.example.test11 D/xxx: 1st=6, packec=-9223372019674906623, type=1 group=4, child=1