In a mobile recipe feed, why do cards jump as their images and text arrive, and how do you reserve each row's size first?
answer
- offsets come from the rows before
- above the viewport moves the reader
- dimensions travel with the item
- a placeholder that never resizes
- measure once, cache by id and width
basics
~20 sA row laid out at one height and re-laid at another pushes every later row, and above the viewport it moves what the reader sees. Reserve the final size from the record: an image box from its sent dimensions, clamped text, fixed slots.
solid answer
~50 sA list places each row at an offset built from the sizes of the rows before it, so when a card grows because its photo or a long title arrived, everything after it moves; inside or above the viewport the reader sees the jump. The fix is to know the final size before the content exists. The feed item should carry the image's width and height or its aspect ratio, so the image box is sized from the record and a placeholder of that exact size is swapped for the photo without resizing. Titles are clamped to a fixed number of lines per card type, and optional parts get a fixed slot. Where height truly depends on content, measure once and cache it by item id together with the layout width and text-size setting, and keep the list anchored on a visible item when estimates above it are corrected.
go deeper
Recall that a row changing height moves every row after it, and that a placeholder of the final size stops a photo from resizing its card.
Explain why changes above the viewport move visible content, and reserve size from dimensions sent with the item, clamped text and fixed slots.
Handle content-dependent rows: measure once, cache by id, width and text size, estimate by type, and keep the list anchored while estimates are corrected.
Treat layout stability as a contract: the API sends dimensions, every card type declares its size rules, and jumps are measured rather than reported by users.
## Why a feed jumps A list places each row at an **offset** computed from the sizes of the rows before it. When a row is laid out at one height and later re-laid at another — its photo arrived, a long title wrapped onto a third line, a sponsored creative loaded — every row after it moves. Where that row sits decides what the reader sees: - **below the viewport**: later rows move, but nothing visible shifts; - **inside the viewport**: the card grows under the reader's eyes and pushes the rest down; - **above the viewport**: everything visible moves by the size difference, unless the list compensates by keeping a visible item fixed. The third case is common when the reader scrolls back up through rows whose sizes were only estimated. Each jump also forces another layout pass in the middle of a scroll, so a jumping list tends to be a stuttering one too. ## Reserve the final size before content arrives The fix is to know a row's final size before its content exists: 1. **Carry dimensions in the item.** The feed item states its image's width and height, or its aspect ratio, so the client sizes the image box from the record alone. Asking for this in the API contract is often the cheapest fix available. 2. **Draw a placeholder of the final size** — a flat colour, a dominant-colour block or a skeleton — and swap the content into it without changing the box. 3. **Bound the text.** Clamp titles and descriptions to a fixed number of lines per card type, so their height is known for a given width and text-size setting. 4. **Give optional parts a fixed slot.** A rating row or a 'sponsored' label either always reserves its space in that card type or never appears in it, rather than appearing late and growing the card. ## When the size cannot be known in advance Some rows genuinely depend on their content — a recipe card whose description is not clamped, a header whose text comes from the server. Then: - **measure once and cache the height by item id**, together with the layout width and the user's text-size setting it was measured at, so scrolling back or rebuilding the list uses the measured size instead of a fresh estimate; - **estimate by view type** for rows never measured — the typical height of measured cards of that type — so the corrections that do happen stay small; - **keep the list anchored on a visible item** while measured sizes above the viewport replace estimates, so the correction lands off-screen instead of under the reader. | approach | suits | cost | |---|---|---| | fixed size per card type | uniform cards, section headers | content has to fit the box | | size from dimensions sent with the item | photos, video stills, sponsored creatives | the API must send them | | measure once, cache by id, width and text size | content-dependent text | one measurement per item and width | | estimate, then correct | rows never measured | a visible jump unless anchored | ## A worked example Assume, as the scenario's own figures, a card 360 units wide and a recipe photo the item describes as 1200 by 900. The image box is 360 × 900 ÷ 1200 = 270 units tall before a single byte of the photo arrives. With the title clamped to two lines and a metadata row of fixed height, the whole card's height follows from the record, the width and the text-size setting, so the list can place it correctly the first time — online, offline, or while the photo is still in flight. ## Pitfalls - **Animating the growth** makes the jump smoother, not smaller: the reader's content still moves. - **Waiting for every image before showing a page** avoids jumps at the price of a blank feed held hostage by its slowest photo, and offline, with photos missing from the cache, may never show the page at all. - **Caching heights by index** breaks the moment an item is inserted above; cache by id. - **Caching by id alone** serves a stale height after a rotation, a split-screen resize or a text-size change. ## Where the boundaries are How the photo itself is fetched, decoded, downsized and cached is the image pipeline's design; the placeholder only promises the box. Keeping the reader in place while fresh items are inserted above them is the refresh-and-merge design's job. Here the concern is rows that already exist changing size under the reader.
- The API cannot send image dimensions for older items. What then?Fall back per card type to a fixed aspect-ratio box, cropping or letterboxing the photo into it, so the size is still known up front. Record the real dimensions when the photo arrives and store them with the item, so later layouts use them. Ask for dimensions in the contract for new items so the fallback shrinks over time.
- Why cache measured heights by width and text-size setting, not only by id?The same card is taller at a narrower width or a larger text size. A cache keyed by id alone serves a stale height after a rotation, a split-screen resize or a text-size change, and the list jumps when the real measurement replaces it.
- Animating the height change removes the jump.
- A size change above the viewport cannot move what the reader sees.
- Waiting for every image before showing a page prevents jumps at no real cost.
- Height estimates are harmless because the list corrects them later.
- A row's measured height can be cached by its index.