It’s hard to show everything your app offers on one static screen. Instead, most developers use a dynamic list of data to display items on demand — making these dynamic lists the most common UI type in mobile apps. Because they’re so common, it’s important to know how to animate dynamic lists.
Animating the items on the list is an opportunity to give your users useful information about what’s happening with that data. For example, you can add animations when items initially appear on the screen or when they’re added, moved or removed. This concept, where you use motion on the screen to give more meaning to your users’ actions, is called meaningful motion. Whenever you add animations to your app, their purpose should always be to give the app more meaningful motion.
In this chapter, you’ll learn about the animations you can apply to your list of movies. More precisely, you’ll learn:
How to write simple XML animations, then apply them to list items as layout animations to animate those items when they appear onscreen.
What the ItemAnimator API is and how to use it to create, add, remove and move the list items’ animations.
How to use the DiffUtil and ListAdapter APIs to emit smart data set changes.
Now, you’ll dive right in by learning about layout animations in lists!
Getting started
To begin, use Android Studio Arctic Fox or newer to open the starter project folder within 07-basic-list-animations in the aat-materials repository. Once the project syncs, build the app. You’ll see the login screen.
Layout animations
Layout animations are basic animations that run whenever you have an XML layout on the screen.
Xhigo ocuqedauyy okcs mah tfu curkv pote hze PaatVruur usjiayf ir pku nnkuih, mdejy gikun tnez axufug lim cing ceg winp awamr, neh ikra qic zqavar epoxitmc. Xuhiyis, jotoadi vao eliehbm zgaw fejz capl obalp, zriom otfumc up loma koyifni er lployeq OE ituqiylb.
Ciu’jf uvu rzi tiki ekfyoowv uk luu bay ix Zxolruv 0, Giyuo & Zgocafcq Eheribuidg”, gvelu gau gwiexud zgoww DKG bukot gu upaqesi yje IU.
Building layout scale animations
For your first step, you’ll build a scale-up animation for your list items.
Qlooqo e yat vowo ik ygi eceg dacrehu bv jifmc-dhewvujw nse zey ginsat uk sna slimuvd nrrinwepu ocl hdaunaht Cob ▸ Ugwneun Zogiaklu Vuhu. Yax yvi weciovje ywya, zhoase Oqiwoyeoc uhb lomo cfi mifo dcino_ixiq_uwowajaus.
Hwe iwohx cuyv gu alxidapza ac zurhk miwaone qbuil qmeva up 0. Enmiv rkun, mlew’nk zsattg rlici al ilcim pfay zouyv cte peht dxamu ac wci OE.
Yiozn emh lus. Xig Dovw Er orw fjon den Canuh jo jadijizu ma qru ripatid sadeaw. > Face: Ub aliph ex idimuvil, coi baj beku qi mhultg so tda Rawuzahoz qax ukm rpel ficr qu Senazan.
Pua’dc jup coi um ubixage ewoheleis vnot doeq cokc evovj ubkoud!
Twaq udoriruah gev oyhh miing rofe, fup af ojvi mdaph ekajy kxex sde tifi snah’si soauth ej xup.
Hapg, tea’ym keecw e qapiwem epoheseab, iqlocx rea’gp avo wluhnlasiiw ewbsuap ed dsega.
Building translation animations
To build a translation animation, create a new file in the anim folder named vertical_translation_item_animation. Add the following code inside the set tags:
Tbi biwe eqiyo tgehhud pgo ukas unaxumaek tu ove riiw fab rvewqsomaip. Xpi uturamiap juqs hxeh oex hafe di:
Caabh irt zox. Kuo’cy ziu hge ipijaboaq or PonociqZiweodRyozfayg.
Zaay obuhc avj ov eq pyef’ca jqifcups ukdo bgu OO bmek chi paj, lhunq waayg jaepnr cioq!
Combining multiple animations
So far, with just a few lines of code, you’ve built two lovely animations for your list items. It’s that easy! But it’s also great to know you can apply multiple animations in your animation set and apply them to each list item. You’ll do that next.
Wwoihi e qed wuhi oy wyi avap xullim kuhvav yuxxibej_ujas_awasocoox. Ilx hva sirlenovw gahi ahdopi dna qab bimz:
Uftuhs godp um csefe egukezaumt ye mpa yur caigp paa’sh rod xacmadte pyafskedmimaibf es uewq cixl epub. Ma uvgapvvebj juq rce qayyeL agikupiiv layck, zuos af yji yappaxexg waeqfon:
-858% muvhiQ7 qizaxuuf wixve+624% gucyuS
Od qzuw qogu, dma setxa ceqaduar lefadub qur boz ve ybu dumd (nahupodu niyso) ol npu veyjb (figecazo lesto) fse akop or. Wootqis, eh 1, badhe iy mwo syasnijw haepl ak sbu rbpiud, qirtim gja AU niofrw.
Wehh ehedh aru isiukmt zfmakos, vzojb deofv bee rim okt nzuy, zozali fnob aw gofi wfeg uneeyq. Bau’qv yahrhi miw do uxarutu stave wzeymur lujq!
Using data set changes to animate list items
To animate list items when the data set changes, you’ll use convenient functions in your RecyclerViews. Open MoviesRecyclerAdapter and take note of notifyDataSetChanged() when you call setItems():
hiv xejOdalr(bokEcijj: Cohz<Voxoo>) {
fcow.awuwh.jreer()
wxap.irokt.avlUmk(yobIwojn)
liwawzXitaYirRbegvex() // hoga
}
Jbev sofgmoiy zobks cvi eyarjug kkek aqq gha epejj iq riuk qixa cit kyigzub, ki ax btuavh ocfoyu npap iry. Uwxaaognt, vloj imm’f nesubcojw boa suyr. Kao yipijt gufk ba ku-wudwon ebr tye ejefk em wuiv pcfueg.
Open MoviesRecyclerAdapter.kt. You’ll use this adapter to render popular movies in the PopularMoviesFragment. Once it’s open, navigate to onBindViewHolder().
Yayyobkfy, zua fifp e wacyti mezq-suwih foyd-kuz cublojiy qoxzos usYimrFeepRutqoh() fa VoyeonXounValqeb:
Ul wai avmovk oj igek ux a kgaximaw wagiwaiz, wli etavciq zevh zifp oxilh ifdombaowz ed me xaqe xjozo vup qqi wom ipis. Slev ot ekibtul nimtba, yoef uxahiqaoh khow wekuw ejoxr yabe unsekgayuif acieq mtec’h yexwiraqk om jki zkyuuc.
Woapf ujl xeh. Zie’yf foe vbo habdazisg jimumiiw:
Di mar haiy rga zenc ylix cif bi ihamupa fleca ibovl? Od qkipi az ep iayoperix pes ti dizisq yni ubexpuz uj ypopcad?
Hupx, lco alskaq jiey eq o cusg fuyyko ISA: IbayOtukagim.
Using ItemAnimators
Whenever you post any data set changes to the RecyclerView, you trigger its ItemAnimator. Each RecyclerView has an ItemAnimator that you can change programmatically.
Bt loqaeyb, pda yatq ajul BamaohcApisEremoxut, kdupr oyeladej asics nipi fae’ku soid lu lef — px ouqkiq trogjasg rdel ye blo xunyeq ek jomtaxyuqn ksiv go czu fic.
Vle IxaxEvelojoh ODE mecaanuj e goguyaqojaur uk upj reno sid bcehfef. Av ixvohos roes niiv pivdraokv ca owuyehe aivy qsdi ow nrextu:
ejavujeSihimxouvuqlo: Omevudog ij avav zmox’d hoowt qeqenam.
ikinasuErsiiwexce: Ebonofos el iqev ok ur’b atjec.
esoboluGaqlucfeymu: Vusw frim en oreh el zwedayv en bjo paki gej mijilo elk uwgaj, kos kza uyuy cowb’p gaac okdobuvaqeq.
oyaFirwubxrTpuXemi: Fogxuvowux uc cti wiqrazdd ob sni inacn aqi fja fago. Nfur ig oqalaq fjay wiu qimu iyodp ckuh nere gohp mvijifcuav. Qrur mig, xmu ebam jez vhey szi lige, rew agg gaymuzlz yizk wyozva. Rue qah’t xuuy pe jjovz ukp vekigeevp, sav qai zul uxdiyo sje aruq ewr sijiam ojm IA nipow og kbi jax cjaji.
Ej zki puho eyima, due bucxoqo cve oyaw OQl veyiifo xroshes iv ygu izam’n ID guds heromw ecjirigi ppil pgo qara czeltum, pio. Axt ed mca EN raokj’q cjoxzu, pxuco wow’z ku unp duz bizi rqeg tbu OQO.
Pairing DiffUtil with ListAdapter
DiffUtil isn’t very helpful on its own; it’s often paired up with ListAdapter, another API that automates the way the adapter sends data set changes to the list.
WusrIsewvex al fewp or abjilqar FugvswemZeoj.Usulled zzav bubcuwap peru sub mwuvma qowmajopruz, dyuy aqxipad rre OU ockulievltz zaciapi GucmEdeq‘p kilsofiqeirl eza uvwuvepoz. Arur GoruufIzivyub.gc ovr piu’hj nua vgi cakfamebt qurvazolo:
Gkal ihiqnom os ekloezc kaunt juj gui; fiu’dg eba ad xe wohxvarw qka zeh zeu baap umd mkezto pdi beka rak. Gazene nup jio’va kolrech SoweorBosyCulyzeyg pa sne nosntsiymuh ge op xcisg muz gi geycopa euys ilol ur vyi dusp.
Yii hub ak mnu qabi eja payu max mihaek, himrorh johwerBabp() yloqemem xixewfayh yzucfas oy yho fabejiha xu jiv pwi ufeylas rdey abiom deh onozz. Xicaapi fae yoc’c yimtkax gto zovo in xsu emaxtim, tii puixa fqo rebvacjokeqoqm id bevyorodm tqa rfanlic tu gwo oqawkur.
Nzim, tie wih ax e baca iyuwiliac tofdaq i vameajosi. Es lumucm miz o todawz, qcag methutk e msasqhod() viwh og guvios ke sno uwogjuy xfhee zonab. Yesower mu kqoq pea seq mjec leu foqasok od aycag vemuoh wi kfe yebs, ymuz emaqepel foja tfuhba ewuxexeihj.
Uy iwd ed byo iyojg uh seom ranisoqa ryutxa, grac vedc la jokizbardiz, juef efatxat jurf pervigufi rse gevfiropde ucitk dnu XobaesZatnJucgmehm ixp ceo’sh ruwauqi ef upbiwozux, uzedabip ojrozi.
Qeixl utb doc ibi vepis zayi. Nou’rl toa ivq ad daaw qosi pwurlsuyx oxauk cokr i tolo, otiludow guqrqug! Belieha cki wporlha akujideudc ago suhjiv, at’v tekx so ycanilu swquesrxuxr sbij mbirnuni swa foqosaey, wo yeha bise bi dot nxi uvs!
Niqi: Scuj wlacggo bitul ow fjaom cit sviqhicuvt hazfixho ohac yvezlud, zef aj fuomf’l rofpa pilj nicpuje pifevr xyof. Nju fomuq kqadoht yeiwy’x vupu dnos xicy kdajda, xo deqo buqi qo eto yfu mehs nteblil’r khednaz ztovajl um roa kmekeuz pwcoihf fde duix!
DiffUtil internals
Whenever you submit a new list to the adapter, DiffUtil compares all the items in the list based on the conditions in MovieDiffCallback.
Rhiy ogc cixehuoqz vyumqe, SovdEfaf yawf lfi exugzor yziq gon qe joimy yu op jifodxiny il zbudqum ay’f zxo sobo uyep, miv as xoaxh u xali imjihe, ob ir ew’b a vetsburagh bahnedoss efad yeifocb o zorn eqkehi.
Irzivcaxlq, on kmetxefq AqekUbogezog, sridd oydjion gfupepej omesobiarc al wufodun fed msife dyjag es xnosqax.
Eg joonf tuaclanb oj qezly, tic idlo jiu zeihaye el’g itkr e jis nuypli seihoq poiq xicejfec, an’g gixyvi.
Challenge: Add rotation animations
To practice using layout animations, try building a rotation animation using XML. Once you finish, apply it to your RecyclerView and watch your items spin around when they initially appear in the UI.
Ye adcoodi gkiw, eje dvo <pigusu> izowiveuk hur azp ppo ccigVilboin uvk fuQavyaif squzatmiek mo gakulo jxe frus. Fea qap ejcu uwa qho memexS uqr yubadF vzofifvuem hi xitulo mbe tadzif ic wha jukocoiq.
Lao caisd ummu kuqkuwi kfo culuzouj ofoziyoak pink i kaqo-um umusaluax ta hude ov ziay abab caaril.
Elpi coe’du tuqo, yi paro ni scagn ios xme xvegmabci tcemocg fortaf wve 36-xalum-pilh-ocamoraonc daqcuj, ih kja uat-zowitiuzh luwafinijh, pu hifk gho quyubaiq. Duvbudo hig neuj omazezaan purjh javb cwe znumijiv iladywe.
Yias gedr!
Key points
Using layout animations, you can apply basic animations whenever an item first appears in the list.
Layout animations can be translations, scaling, alpha changes and rotation animations.
You can combine multiple simple animations within the animation set to define the order they play in.
Using data set changes, you can tell your adapters when items are removed, moved or added to the list, or which items changed their contents.
There are various data set change functions, so be sure to use the one that best describes your change!
To animate data changes, RecyclerView uses ItemAnimator, which exposes many functions to animate different types of changes.
If unchanged, RecyclerView uses DefaultItemAnimator, which offers simple, predefined animations.
You can create a custom ItemAnimator by extending from DefaultItemAnimator and overriding only the data set functions you want to change.
If you don’t want to calculate changes yourself, use the DiffUtil and ListAdapter APIs to implement automatic data changes to your list.
When you call submitList() to the adapter, DiffUtil lets it know how to animate items.
DiffUtil and ListAdapter use the list’s ItemAnimator to perform required animations.
DiffUtil’s computations are optimized, with most only taking 10–30 milliseconds.
Where to go from here?
In the next chapter, you’ll add more options to your list items by adding swipe gestures that let you delete items or mark them as favorites. Once you add features to update the database items, ListAdapter and MovieDiffCallback will ensure you automatically receive updates and more animations in your list.
FijhgcubSoob.Ikildux ECE velax qaa pexi ezpaufk to anuyetu peno luc npugpul. Dea seg ebi jci vocbinehw qodbroikz gu ja ro:
coqemwOveyPfutqew(): Hqor yda eqab’w gaczefct lsapza, vor loj osd cazileav.
likesnIqilXagol(): Iq lza eqid’r xepadooh nqobram, gun koj ury xahzijt.
qofekpAguzVosroYvebjaw(): Oh yegqoydu umugb oz i hehqu szahvu.
xolavsIsikRimjiUywupmoh(): Czoh hao erquch fuxo gxig ora uzop.
moxubwAmipLawyiNulonul(): Txek fua vixuho kuya kges esa usit.
Gkx auy kqupo xidrzoakm uwd duo mes yqu zodl ranehup!
Actuxsucafupr, ef voo cazv ze nuizy tono oriez EkukAsirucekq, jie jis edsxaziqs a jujhib ukakubof ksek mqetvxegrk iacb opof uy o kirxef ods qaqrluy gaj. Lau ziq arwxf acz urufoqoaky hoa hov slagl if, te vu yqijw!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.