Skip to content

Layout Manager Implementation Details

Tonic Artos edited this page May 5, 2015 · 11 revisions

Sorry, this is out-of-date and incomplete :(

Details on the implementation of LayoutManager.

Contents


Layout

How headers are positioned

Scrolling

A user scroll is interpreted as either a movement towards start (Direction.START), or towards end (Direction.END). This movement is given as a value dy, and is the user requested displacement. The actual scroll is the computed value delta, governed by the relationship 0 <= delta <= dy.

The scroll result is the difference between delta and dy. This result tells the recycler view how much the user has over scrolled the content, and is used to display a graphical 'overscroll effect' to the user, indicating content boundaries.

Computing scroll distance

The dy value passed in scrollVerticallyBy is the distance the user is attempting to scroll the view by. This distance may be larger than that we can scroll by, depending on the position of our content views. What needs to be computed is the actual distance we will translate our views by.

The approach used in SuperSLiM is to fill out the leading edge of the scroll first. This fill is actually to a point before the leading edge, either 0 - dy for a scroll towards start, or height + dy for a scroll towards end. By doing this we can work out if we have hit a content boundary by checking if we have laid out the first or last view (plus some special handling of headers). If we have laid out the first or last view, we can get the leading edge of the section and compare it to dy to get delta.

Once delta is computed we just apply it to all the child views.

Managing views

It is assumed that, unless sections intersect the top or bottom edges of the recycler view, sections do not adjust the display of member items. This assumption allows for the translation of views belonging to these sections without interaction from the section's SLM.

Headers

Headers only need to be updated for sections that intersect the top edge. See how headers are positioned.

Leading edge

To help with computing the scroll delta, the leading edge is computed as either 0 - dy for a scroll towards start, or height + dy for a scroll towards end.

SLMs are expected to fill out views from an anchor with an incrementing or decrementing position depending on the scroll direction. They should not add views which are wholly positioned after the leading edge. The fill result gives the sections leading edge which is used to compute the scroll delta.

If a SLM returns with a value before the scrolls leading edge then it may be that another section needs to be filled out, and we keep doing this until we run out of sections or a returned value is after the scrolls leading edge. If we run out of sections, then a content boundary has been reached and we compute the scroll delta.

Trailing edge

The trailing edge is resolved after the scroll translation is applied. This means the SLM for the last section is expected to trim (remove and recycle) views wholly after the trailing edge. In this case the trailing edge is computed as height for a scroll towards start, or 0 for a scroll towards end. The trim result is a boolean indicating if any views remain in the section, if none do, we trim the next section.

Scroll indicator

The scroll indicator is an optional display mode toggled in the recycler view. Layout manager supports two versions of the scroll indicator, the default smooth version, and a plain version.

The scroll indicator is positioned and sized using three values, offset, extent, and range. Offset is the distance, in terms of range, from the top of the scroll display area. Extent is the length of the scroll indicator in terms of range. Range is the number of steps along the allotted length of the scroll indicator area.

Plain

The plain version is an option in performance constrained situations. There is a slight complication handling headers, but otherwise it is simple. The offset is the position of the first item. The extent is the number of views currently displayed. The range is the total number of items.

Smooth

The smooth version still uses item positions, but adds fractional amounts as the user scrolls past items. The extent is computed as the number of views displayed, minus the fractional amounts off-screen. SLMs on the top and bottom edges of the screen are also queried to determine any items that are not displayed, but should still be counted as present.

As the computation methods return integers, the values are normalised to the recycler view height so as to not lose any accuracy.

This approach gives a very smooth and accurate scroll indicator.

Versioned Pages

0.4

[Getting Started](Getting started with version 0.4)
[User's Guide](User's guide for version 0.4)

version5 (WIP)

User documentation
[Basic usage](A simple tutorial)
[Advanced usage](All the cool things)
[SuperSLiM and RxJava](Using SuperSLiM with RxJava)

Developers documentation
Glossary
[The section graph](The section graph)
[Tracking data changes](Tracking data changes)
[Configuration transformations](Configuration transformations)
[Layout helpers](Layout helpers)
[Section configuration](Section configuration)
[Section state](Section state)
[Section layout managers](Section layout managers)
[Header layout managers](Header layout managers)

Clone this wiki locally