How to preserve scroll position in an ExpandableListView

后端 未结 4 467
走了就别回头了
走了就别回头了 2021-01-31 21:44

in my app I have a class derived from ExpandableListActivity. When I scroll the contents, and then change phone orientation or edit an item and then go back to the list, the ori

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 22:20

    This is how I got ExpandableListView.onRestoreInstanceState and onSaveInstanceState to work...

    If you're using BaseExpandableListAdapter the default implementation of getCombinedChildId returns a negative number. If you look into the code for restoring a list from saved state scroll position it ignores ids which are negative. Return a positive IDs so that the AbsListView onRestoreInstanceState will set the scroll position properly. To return positive IDs change the or of getCombinedChildId from 0x8000000000000000L (negative) to 0x7000000000000000L (positive) by over-riding the method in your subclass of BaseExpandableListAdapter as follows...

            public long getCombinedChildId(long groupId, long childId)
            {
                long or  = 0x7000000000000000L;
                long group = (groupId & 0x7FFFFFFF) << 32;
                long child = childId & 0xFFFFFFFF;
                return or | group | child;
            }
    

提交回复
热议问题