/// <summary> /// This is called when a cell is clicked /// </summary> /// <param name="dataIndex"></param> private void TweenEnd(int dataIndex) { // toggle the expanded value _data[dataIndex].isExpanded = !_data[dataIndex].isExpanded; if (dataIndex % 2 == 1) { // single expanded cell. collapse others for (var i = 0; i < _data.Count; i++) { if (i != dataIndex) { _data[i].isExpanded = false; } } } // Since the size of the scroller will be changing, we can't simply pass in the normalized // scroll position to the ReloadData method. Instead we will do some math to get the current offset // of the first or last visible cell and use that in the JumpToDataIndex call after the ReloadData call. // This can accommodate any changes in the sizes of multiple cell views. var jumpDataIndex = 0; var jumpPosition = 0f; var jumpCellSize = 0f; var cellOffset = 0f; if (_tweeningFirstPadder) { // get the end data index so that we can jump to this after the reload jumpDataIndex = scroller.EndDataIndex; // the actual start position of this end data index so we can calculate a cell offset when we jump jumpPosition = scroller.GetScrollPositionForDataIndex(jumpDataIndex, EnhancedScroller.CellViewPositionEnum.Before); // get the size of the cell at the end data index jumpCellSize = _data[jumpDataIndex].isExpanded ? _data[jumpDataIndex].expandedSize : _data[jumpDataIndex].collapsedSize; // get the cell offset by taking the difference of the bottom of the scroller and cell view start position (minus the bottom padding) and dividing it by the size of the cell cellOffset = (scroller.ScrollPosition + scroller.ScrollRectSize - jumpPosition - scroller.padding.bottom) / jumpCellSize; } else { // get the start data index so that we can jump to this after the reload jumpDataIndex = scroller.StartDataIndex; // the actual start position of this start data index so we can calculate a cell offset when we jump jumpPosition = scroller.GetScrollPositionForDataIndex(jumpDataIndex, EnhancedScroller.CellViewPositionEnum.Before); // get the size of the cell at the start data index jumpCellSize = _data[jumpDataIndex].isExpanded ? _data[jumpDataIndex].expandedSize : _data[jumpDataIndex].collapsedSize; // get the cell offset by taking the difference of the scroll position and cell view start position and dividing it by the size of the cell cellOffset = (scroller.ScrollPosition - jumpPosition) / jumpCellSize; } // if we are expanding the cell view, add a look ahead to the beginning and end of the // scroller so that it loads in extra cells. This is necessary because when we // start to collapse, we will need these extra cells so that the others do // not get stretched by the layout element of the ScrollRect container. scroller.lookAheadBefore = _data[dataIndex].isExpanded ? 200.0f : 0.0f; scroller.lookAheadAfter = _data[dataIndex].isExpanded ? 200.0f : 0.0f; // reload the scroller to set the new positions and sizes scroller.ReloadData(); if (_tweeningFirstPadder) { // jump back to the original end data index that we cached above to make it // appear the scroller did not reset. // we use the cell offset calculated above and ignore the spacing. scroller offset is at the bottom scroller.JumpToDataIndex(jumpDataIndex, scrollerOffset: 1f, cellOffset: cellOffset, useSpacing: false); } else { // jump back to the original start data index that we cached above to make it // appear the scroller did not reset. // we use the cell offset calculated above and ignore the spacing scroller.JumpToDataIndex(jumpDataIndex, scrollerOffset: 0f, cellOffset: cellOffset, useSpacing: false); } }