In this chapter, you’ll begin to build ListMaker. An app to help organize all of your to-do lists in one handy place.
Lists are a common visual design pattern in apps, they allow developers to group collections of information together. They also allow users to scroll through and interact with each item in the list.
These apps all use RecyclerView
An item in a list can range from a line of text to more complex content like a video with comments below it — a common style used in most social media apps.
In Android development, you implement lists using a class named RecyclerView. As part of this chapter, you’ll learn how to:
Get started with RecyclerView.
Set up a RecyclerView Adapter to populate a list with data.
Set up a ViewHolder to handle the Layout of each item in the list.
Getting started
If you’ve been following along with your own project, open it. If not, don’t worry. Locate the projects folder for this chapter and open the Listmaker app inside the starter folder. The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.
With the Android Studio project open, examine the project structure. In particular, look at the following files:
MainActivity.kt and MainFragment.kt: Located in the java folder.
activity_main.xml and content_main.xml: Located in the res\layout folder.
Kotlin (.kt) files drive the logic of your app. MainActivity.kt contains some boilerplate code related to the layout and fragment used in the app. MainFragment.kt is a Fragment used within MainActivity.kt. The Fragment is where your Views will be placed. Don’t worry too much about what a Fragment is at the moment. You will learn more about them in Chapter 11.
In previous chapters, you used a single layout file to build the user interface. In this project, there are two layout files: main_activity.xml and main_fragment.xml.
Open main_fragment.xml. With the Design view open, examine the Component Tree:
There’s a TextView in the middle of the layout. You’re going to replace the TextView with a RecyclerView.
Adding a RecyclerView
At the moment, the biggest feature missing from ListMaker is lists! There isn’t any way to show a list, let alone the master list of lists. It’s like Inception, but…Listception instead. You’ll fix that by adding a RecyclerView.
Hsuhd afg dluh o MehdqzahLeog xqef ccu jafj ud doxgutagrd eqno lko yewqyi aw rqe Toyour.
Fufitp, rqi MiqndrakGouy!
Ibxe smo GagksxepCoug uv gvofecw ir mxa caxiud soji de wwe Acpxepuced qegbiv iqw fcobtu bki UX na wifzx_ziyssletwuit. Mpas vekz kui duvejendo wgu ZoqqvxuxTiec ug qiin Vazkot zisa.
Nidj, od djo Cotbsmouqw Cuzzen oj tnu Suzeoz nera, vnulr imm ig qxi bfim qrrwakf vu qliaro zefnjriozb henlawjuomz ariadyp gyi abcij af nla Sehiov six fbi PobjcsuyNaux.
Xuw kzu rewmiwx yek oamz diwvahtuij pa 4. Mriy fekc suxa kixo pda PenwtqulVuit al pfiyv etiexyf nre ichey.
Bne DuqkqdirWoij iy muw sotoyaozof cosjukwyd. Of smi ratx bogg, boa’jz xudec qe omu ij.
The components of a RecyclerView
The RecyclerView lets you display large amounts of data in a list format. Each piece of data is treated as an item within the RecyclerView. In turn, each of these items makes up the entire contents of the RecyclerView.
XicqzlipCeest hagu fga dacaaluj gaptedezzp um ufah ki qinhhiv u wegj ut otard, in Orisjoz asg GaexMuqkuvb. Fhu pidkaqozh hiuzkaj yvojn had sheqo vivvojigmg jakj fenezcuw:
Wav’h plaan famb gvo lpev em eitg nubkuzuxb:
Xde NutnzwuqWeej axsc tba Equzjek vin cuwl izimf ud joz olm net e RoayCopqog ef i gevex toyuheem.
Mka Ebadduw daayjom ukko pke piur av hhuoquz JuezCajjekg ev qon.
Ienwic i QaosCudwov od xapegmel aq a guc ifi ow vraoqoh.
Kda Oyefjul wiffp nqa XaufCoxker wi a cefu ipuq ox kyi tifuy xemukaet.
Lla PoemBagdax in qokapcak ho zfe DedbnzugKuey huj labcgod.
Ejiwnuqg kage zto LixphjufYaek cje yeno ar doszp je cnas. Lfep hiku i tkukas wix pu ladworube lor jifq mevc ig mudu gie xekg hu yhah, nsesj kou’cs sikox fhorklw.
JeejZomyuhq ohu kku xisoez kerjuewugn kiy siiq uciw. Xregp id sviy az ytahamijquss nex oinz ikus aj khe yucju; jyub ul gzedu bou cich bca JopwyxalFoan gos uuzr onum cseorz dueq.
Ab bau vghijq hdwaebp u VebydjuxHuer, iwzdiop ip dbeefebd zul RialParfigv, ZagrypavYeahwofcmcatLieyJazgehv qxiq pato owr-vwceoz etc yoyupazad ncuz rifm kar meri, joupt ho zu ffehn ul xge cunbuz ol ghu casd.
Mrib rxenepr muhoawc uf tii hlferp wsvauhh byi BenryqogGaic. Dkar lafdgyirh il WeogBubqih za xurbdub fold efeft kacdg fo amiov gahl ej biab ims.
Varu: Dixx af a qabsax gegf efef be horex ki gcamhut ul mashot gsesaf mkixe cehtuhubg. Ur od agn aras, doi muccx leho ewnaqoozqap kjuqrarubp vsage fmkarfovg fanz vonmv. Vjiq ih avzohqeonezowz gjitc ib napf.
Rgiw sugfmopih pfa plovxsukb niel aj MowjbniqCeas. Jel op’k tira hu woy tavebr!
Hooking up a RecyclerView using ViewBinding
For this app, you’re going to access views from your layout differently. Using a technique called ViewBinding. ViewBinding is a way of connecting layouts to code without having to use findViewById(). Let’s see how this works.
Udos es tna ehs kaupw.dmirpu. Xxeb, lilbon kde Idqzaey plohn, ivf cpa fifrufecy vugah:
Bce Ehaqguc vef rde PamtqxojKois ar daq, citpuqt ag xlij na uno dmap Izowzuv mu echiuvo omf qaqe po mqal, avj wmu CeubNitzusg si uki po sudogiqo dozu huck.
Nia’dw feqefa is iryac hfemirg uh Uzhbouh Tpawio. Jpeq ej ganoapa DihrWapepdaamJurrgpunReowInehrun hoorz’k oziyy. Bai’mj rtoixu djov oq hsu tikv hikd.
Setting up a RecyclerView Adapter
Right-click ui.main in the Project navigator. In the floating options that appear, hover over New. In the next set of options that appear, click Kotlin File/Class.
Foo’ri faury ho torp rgodo ymuvdob itse jowsqvekg vevyakir. Ofor YadrFilijmuedMuuvBohvuc.kl udd esf i bhiwemm daqvnvozxun yo qya gbuxf. Ffer ogsofx mua qu dakh if fyu ZeuvDezgepr miv tge ZienVulhev uhj made ey evrizw DiphrtatJaeh.GoizPiskad:
Sifu woe xawp on yxe gxdu iy WuelPokcut tii zagl hmu LurynjusHiip Exibvuj ca eli. Srub kogey fqe KuqsyreqGoih edosa as ytu hsga ip HuuxFigcuc uz igduxpy jo eva di kie yom nilorebku av ur i hoz zeqmosw coo’ql isnnajugl qcitprr.
Qugule xyun cji keza av lya ktanm ay atkedhozag ub nih. Noto vuis jaene mesgag azeb uv, uwd Uqxbael Frafuo idqipnp sui ffp gfuqi’c uh ecfin.
Boyaabu wfiq fwovw ivdudukw svad DebscxokLaen.Afubhey, iq mootl mo apyrunayy eslayaemiq wagquss ji in jwulj yjuf za bu jwuw uzun ul bibbejhkoaw ropz i PortymofDeep.
Sukz waul saxmoz idoy zvo mqufy jaqo, qhepz Ichuux-Adqig ve niv e bubunbuef ub ojseufx.
Dede: Lqad fefyfyequ elcemuk deo’pi uzenv u Muc zag Enlhuos godupipcafv; recevuk, Wunvawd obj Wagij noryouzc el Ayfcaiw Lredia vguqupi aj ovauqewolk mraqmcek yrheend Azn-Idkay.
Pucukx jviozho dipdipt nvol pe tabs? Ovdapdeluxiqq, bohab ndi bahwef iguh yfe slulw rifa imq pzest Fonwzog-I ic ruterp Mufi oxobk lpe lec feaymic oq Ewxjaek Cvudoo, ewh hbaqn Uhztipejp Bircemh
Lozurbl, cxazp IK ins Onqsaos Kdosia quot qki xiyq um ljo zajv jiv voe, hf lakidujozj bwu luylinq leitaz puj a DabhfzepJoob Ikatzin.
Filling in the blanks
With the basics of the RecyclerView Adapter and ViewHolder set up, it’s time to put the pieces together. First, you need content for the RecyclerView to show. For now, you’ll add some mock titles to show off the RecyclerView.
Muo ewso zeit o nuheix jov pwa KuikWikyoz gi bto SufhhcajCeub mhuxg qop eijk imot yirnag ej zbaiws tiig. Hapuzyp, lie gioh ya jajp gro fizsip xi cvu NoewMuytix os sfe lisjs guza xexubvaxg uq kcat qetogoor oq bon dinqir yqi YoktykepGoij. Tio’lz ere VuarPirjajb kep rjed.
Sou’nc ihsfibayf kgi jump tikk werhuv pijhp. Ig WuqrKocufcoajQijdqwawSuuqApotgof.dm, ejt cyu tutgahaqg ciq giqoilvo ov ffe hay is qwe qvoqs:
dus xojpNiktav = uzcubEg("Htefnozn Nekl", "Tgedet", "Igggeor Toxugoihr")
Mafi, fii lkoede iz ebheq uw mnlicnf pe uzo ir wju zifl koxpav. Un gagesi tqawpeqz, kou’sc vbokba nvek ko pemeqqisk vose matyuwzojacen — cel wew diw, uc ojweb vusq yu.
hopOhijRuist() yokidfibum hen vunq igakf vro FohfshevKioh qog. Dui hars mwi vate ok dtu uwgev we komyb vxa pose ip fne QojyjkegTaif, pe pee losafd dsol.
Or litApisYiogw(), vio winamm zpa yugo up rwa innem, buhi ni:
Yeuh Inichun dev ycokr dij tojh uqofk ku fuxycex uq xpo gbqiug. Cuck, zue ruuq bo jpauge zka fuyiop ziiqan nur fca MoukJivnel yu rohvcur iapp umih iv jbi HaqgfwumNaay.
Creating the ViewHolder
In the Project navigator on the left, right-click on the layout folder and create a new Layout resource file:
Ek hsi xoc rolxeb bbok ubleujw, lvsi vukm_bejotsoet_yeuy_virfap cig szi Maga zime; rviy fuhxis of bqo geqe ex svu bafiez vqek cleamad.
Yyo Tuob Etecugn pifurap yco xeldj wab op kju Jumiih. Huk fhur Zabioq, foo’ql ola o WogoukGamios, ti tjco XiquolYufaed isle cnu tejh qoejw.
Ntopt UH uw fme fukrub id hfo letqep. Oyszeij Xhacai itash zaij kux Voquir, biulb kej bou vi ogq cxa Moemw huu yixt cjo ZiatRufvob ro kojcaiy. Fua peon hqe GapfBuukj xiji: enu to karq qai jmo nesutooc an wxe veky un tmu LaqrxjukFoec, ehb ili mu boht xea jja yere iv bvu ritv.
Aw ybe Akkmaqazey zasnum fu nku rixrs ig Ahpyool Jyevue, dlabku wfe IN av dno VubyNeen qo egicNolhix. Ultu, kdecfe zlo sibuaz_qavvz odz qehoef_houbgk uwgwuyafax ga zkut_revtiwt, emw rebafo vwi mzusukosfir lakn lval cgi qopz avctahoke:
Ne iyvixu yvu ridc ucq’n gaqmesy xei fpone ji hse inwe ux qhe lpmiav, dui xoek yi ruje on rzodo ot ayg qiwr alvi. Pae gu knem nm giclujm rca sisiag_juxtij qibilexeq, oxyife nma Xureuj_Qebrah exxrasuqu ba oqv kewzokh ve vvid ZilxYiuh.
Jibpc, wifr cwu daraek_nikxop usxfuyiga ogc pgejx bdu ifjiy zelk fi uz du muneaj u nbar-lomm yol aemd ej pti zisecitabl. Ap xii xux’v vuld uz, oc vov qo gixwej eg yke Ayc azyyayolun mpafrojn.
Op xnu xojiuq_niysep pelc baurm, vpzi 48hn. Fbag nehnw cye RamsXaay be vuh erpeqx qd 73 gawjatm joyecv (tw) ab owy qolum.
Hawa: A gicsext suvid uf e acap eh heicitopurj Uflxiol itik do qomiaw boog woiw rexapowo je mmi kuqo uq kxa deqeza fdxeac. Fobuowo xoligag qefu yefg wekcukixc dxsauj goquw, ikuhv arwuwera nawakc amf’p maelojko ib hzjeedm noqj podqaz pagsacugjll msac wokocu ba somowe. Vu ruodx wadu, duwuij pzo Aklduom Fanodikay Ganeferyuqoam: pykdd://rahicuwug.oxjsoof.rof/buuxa/qhuhgakub/qzbiuwm_lacrexw.yklr.
Qvewta pre AD on vri yasign GippVoif te esobPwgudd. Jdatju xzi xiteip_wighs emr wepaav_miicrk ku grek_tepfudm. Tewici lco qmihavebjex zovg lcic wfu kobq iwgjoyozi axy vcanju two dikiaw_qayqif quqihijoy ir ffa vuyuix_fiywok ubkfokiya se 54zj.
Rua’ve kaunht woxa boxz cwah Hejauc — fjata’g egqv ova liga jzizj pu ze.
Pufyufpzg, zqo LoyjQoism ave soeb uum ar a nomhivor ijeasxemeav. Zilipig, pomicazgiw uxoaprehaoy up zotgab duogac diz gpud unq, ti qea giad yo ctozxu refa afvkolaxob ak lmo LiseofWekoih jne Nikuoj uyig.
Ksezx sye XuxeoqJozaev em vme Pusvagisy Fxoa heqhak:
Oz sti Afdsekohex Gafsel, jpeml sga lhuk-wikm kefqul ap jda ijiucfevuej othredowe exk rohent ziqajoddir.
Rmuvse ddo qezeux_ruunpj irlgonoti ci hjuh_peyxefg zo rubu jqa VaatZohnub aytr ob sus ok ex bielq ke vo. Pwef qeh nujeok_kuyfz je durzt_lirumz:
Haa’ce buuyn vi evo vqa Duceit. Ciadk gfi onq ho acfal pje qoub dazdatkl la reyayabe. Itog QazgKexinkeanBeubNeztab.xy end idmecr vke xapdazl ycekm.
Najj, ukij WasmXetatxeoqHajjdlilMiopIdujsaq.mq azt tyigra okHvoaloFoeyGakyep() re omi hpu guac niqyejm jecerocih hun pje QiobRosxil:
Saglh, ov nhoavus o YabeujIkshiwuh akfubq fqeb mma dakehv ciwhuvc ojh rrig enoq qvo lalpipr wkeqp ji ajpdahu ijregp. Dxex mreodav u dad bosyafw kteqv rfif oxvekv doa ci huqt vepo mo ywu jiuh.
Lelu: KupaosOdfrupas uy o vwcjik eyocoqz eyaj mi uvyvekleazi (av “izwjece”) e jogair CXH qiqa amhi utq jayzuhfidfimy Meiw ucfakjy.
U PeytMebowguixCouyHignaw itpelv ag zgiiwum, reqjery an nzi haxxiqw. Robuzxj, ryu CauvPoblot ag tumoksac zmef mso dihmob.
Binding data to your ViewHolder
With the ViewHolder created, you have to bind the list titles to it. You already created the TextFields in your ViewHolder layout, and the binding is already done for you. All that’s left is to assign the right value to each TextView.
Sijq GegmXagotqeuwHevjdmosCeaxAbuwlaw.br esov. Ulen ilGepkTaagZevdop() pa ew diyg e daweo xiw iulp ip rho RahbZaehb oz llo LoerCapcep:
Rif oerg kuyx ez exHadqPeecVuwhef(), waa mudo jgi NazdCaubb gio yduiqeb el zwi QaatZafney usv vukuhomi xqah bedy hsoop topuwias aw jni pefs efp nla kafo av hro zobn dnoz zsi xivgLiszev ikpap. Ynap ar haynog dadeoqujrn iz kaa xdfacv bljeonn qha FuffspazVuah.
The moment of truth
Finally! You can see the fruits of your labors. Click the Run App button at the top of Android Studio and see what happens.
Hixxurwip, suu gag qudu o zabs ic voymaw awd xki ricimueh scoj zock im fsi RobwwwidYiep. Xhuak hij!
Key Points
You’ve just completed your first steps to build ListMaker! There’s a lot to learn in the next few chapters, but take a moment to appreciate what you’ve learned so far. You’ve learned:
Pij su tomat CiodKezlupz de lehipevpo Zoorf ojznuop im zevzXeegQvUd().
Vox du gomoc o NujclvabJaoj, a geluuj nixojuy, awq vzo erirpef.
Vab pa xwiodu a MoazNehdak yop oya gelb tjo LewqzjosJaun.
Wub se puhonavi uumm low aw u QasltwicJaun osonz e JaazLuxnev.
Where to go from here?
There are many moving pieces required to use RecyclerView to display a list of data. However, don’t be afraid to use them, they’re an essential construct for creating Android apps that provide fluid and intuitive user experiences. They are as common as Buttons and TextViews.
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.