Site icon C1CTech

RecyclerView Example In Kotlin

<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>What is RecyclerView&quest;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span> widget is a more advanced and flexible version of <span style&equals;"color&colon; &num;008000&semi;"><strong>ListView<&sol;strong><&sol;span>&period; This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views&period;<&sol;p>&NewLine;<p>Here I would demonstrate you a working example of RecyclerView&comma; with some basic functionality&period; The RecyclerView we are going to design contains a list of books displaying the book Image&comma; name&comma; and author&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;0000ff&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;RecyclerViewKotlinExample"><strong>Here<&sol;strong><&sol;a><&sol;span>&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"900" data-videoid&equals;"A8MmmRm34K4" title&equals;"Android RecyclerView Example"><a placeholder href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;A8MmmRm34K4"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;A8MmmRm34K4&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android RecyclerView Example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; In Android Studio&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong><&sol;span> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and proceed&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> and add recyclerview dependency <span style&equals;"color&colon; &num;333399&semi;"><strong>com&period;android&period;support&colon;recyclerview-v7&colon;26&period;1&period;0<&sol;strong><&sol;span> and rebuild the project&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>dependencies &lbrace;<&sol;strong>&NewLine; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; RecyclerView<&sol;strong><&sol;span>&NewLine;<strong> implementation <span style&equals;"color&colon; &num;333399&semi;">'com&period;android&period;support&colon;recyclerview-v7&colon;26&period;1&period;0'<&sol;span><&sol;strong>&NewLine;<strong>&rcub;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong>3<&sol;strong>&period; I have refracted <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> by the name<span style&equals;"color&colon; &num;008000&semi;"><strong> activity&lowbar;booklist&period;xml<&sol;strong><&sol;span> in which I have added one <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span> with the following basic properties&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;booklist&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><em>&lt&semi;&quest;<&sol;em><strong>xml version&equals;<&sol;strong><strong>"1&period;0" <&sol;strong><strong>encoding&equals;<&sol;strong><strong>"utf-8"<&sol;strong><em>&quest;&gt&semi;&NewLine; <&sol;em>&lt&semi;<strong>RelativeLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;<&sol;strong><strong>"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine;     <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;<&sol;strong><strong>"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine;     <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>tools<&sol;strong><strong>&equals;<&sol;strong><strong>"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine;     <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width&equals;<&sol;strong><strong>"match&lowbar;parent"&NewLine;     <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height&equals;<&sol;strong><strong>"match&lowbar;parent"&NewLine;     <&sol;strong><strong>tools<&sol;strong><strong>&colon;context&equals;<&sol;strong><strong>"com&period;example&period;lenovo&period;recyclerview&period;activity&period;BookListActivity"<&sol;strong>&gt&semi;&NewLine; &NewLine; &lt&semi;<strong>android&period;support&period;v7&period;widget&period;RecyclerView&NewLine;     <&sol;strong><strong>android<&sol;strong><strong>&colon;id&equals;<&sol;strong><strong>"&commat;&plus;id&sol;recycler&lowbar;view"<&sol;strong><strong>    android<&sol;strong><strong>&colon;scrollbars&equals;<&sol;strong><strong>"vertical"&NewLine;     <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width&equals;<&sol;strong><strong>"match&lowbar;parent"&NewLine;     <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height&equals;<&sol;strong><strong>"wrap&lowbar;content"&NewLine;     <&sol;strong>&gt&semi;&NewLine; &NewLine; &lt&semi;&sol;<strong>android&period;support&period;v7&period;widget&period;RecyclerView<&sol;strong>&gt&semi;&NewLine; &NewLine; &lt&semi;&sol;<strong>RelativeLayout<&sol;strong>&gt&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Writing the Adapter Class<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>After adding the RecyclerView widget&comma; let’s start writing the adapter class to render the data&period; The RecyclerView adapter is same as ListView but the override methods are different&period;<&sol;p>&NewLine;<p><strong>4<&sol;strong>&period; Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>Book&period;java<&sol;strong><&sol;span> and declare bookName&comma; authorName and ImageResource&lpar;for Image&rpar;&period; No need to set setter&sol;getter function as it creates in its own&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Book&period;java <&sol;strong><&sol;span><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>package com&period;example&period;lenovo&period;recyclerviewkotlin<&sol;strong>&NewLine;&NewLine;<strong>data class Book&lpar;var authorName&colon; String&comma;var bookName&colon; String&comma;var imageResource&colon; Int&rpar;<&sol;strong>&NewLine;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong>5<&sol;strong>&period; Create a layout xml named <span style&equals;"color&colon; &num;008000&semi;"><strong>book&lowbar;list&lowbar;row&period;xml<&sol;strong><&sol;span> with the below code&period; This layout file shows a single row in recycler view by displaying book name&comma; image&comma; and author name&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>book&lowbar;list&lowbar;row&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<&sol;strong>&NewLine;<strong>&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;margin&equals;"5dp"<&sol;strong>&NewLine;<strong> android&colon;orientation&equals;"horizontal"&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;ImageView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;img&lowbar;book"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"100dp"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"200dp" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;LinearLayout<&sol;strong>&NewLine;&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;marginLeft&equals;"10dp"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;marginTop&equals;"10dp"<&sol;strong>&NewLine;<strong> android&colon;orientation&equals;"vertical"&gt&semi;<&sol;strong>&NewLine;&NewLine;&NewLine;<strong> &lt&semi;TextView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;book&lowbar;name"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;textAllCaps&equals;"true"<&sol;strong>&NewLine;<strong> android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"<&sol;strong>&NewLine;<strong> android&colon;textSize&equals;"22sp"<&sol;strong>&NewLine;<strong> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong> &lt&semi;TextView<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;author&lowbar;name"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;textSize&equals;"18sp"<&sol;strong>&NewLine;<strong> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<&sol;strong>&NewLine;<strong> &lt&semi;&sol;LinearLayout&gt&semi;<&sol;strong>&NewLine;<strong>&lt&semi;&sol;LinearLayout&gt&semi;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong>6<&sol;strong>&period; Now create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>BookAdapter&period;java<&sol;strong><&sol;span> and add the below code&period; Here <span style&equals;"color&colon; &num;0000ff&semi;"><strong>onCreateViewHolder&lpar;&rpar; <&sol;strong><&sol;span>method inflates <span style&equals;"color&colon; &num;008000&semi;"><strong>book&lowbar;list&lowbar;row&period;xml<&sol;strong>&period;<&sol;span> In <span style&equals;"color&colon; &num;0000ff&semi;"><strong>onBindViewHolder&lpar;&rpar;<&sol;strong><&sol;span> method the appropriate book data &lpar;authorName&comma; bookName and bookImage&rpar; set to each row&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">BookAdapter&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>package com&period;example&period;lenovo&period;recyclerviewkotlin<&sol;strong>&NewLine;&NewLine;<strong>import android&period;support&period;v7&period;widget&period;RecyclerView<&sol;strong>&NewLine;<strong>import android&period;view&period;LayoutInflater<&sol;strong>&NewLine;<strong>import android&period;view&period;View<&sol;strong>&NewLine;<strong>import android&period;view&period;ViewGroup<&sol;strong>&NewLine;<strong>import android&period;widget&period;ImageView<&sol;strong>&NewLine;<strong>import android&period;widget&period;TextView<&sol;strong>&NewLine;<strong>import android&period;widget&period;Toast<&sol;strong>&NewLine;&NewLine;&NewLine;<strong>class BookAdapter&lpar;val bookList&colon; List&lt&semi;Book&gt&semi;&rpar; &colon; RecyclerView&period;Adapter&lt&semi;BookAdapter&period;MyViewHolder&gt&semi;&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;&NewLine;<strong> class MyViewHolder&lpar;view&colon; View&rpar; &colon; RecyclerView&period;ViewHolder&lpar;view&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> val authorName&colon; TextView &equals; view&period;findViewById&lpar;R&period;id&period;author&lowbar;name&rpar;<&sol;strong>&NewLine;<strong> val bookName&colon; TextView &equals; view&period;findViewById&lpar;R&period;id&period;book&lowbar;name&rpar;<&sol;strong>&NewLine;<strong> val imageView&colon; ImageView &equals; view&period;findViewById&lpar;R&period;id&period;img&lowbar;book&rpar;<&sol;strong>&NewLine;&NewLine;<strong> fun bindItems&lpar;item&colon; Book&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> authorName&period;setText&lpar;item&period;authorName&rpar;<&sol;strong>&NewLine;<strong> bookName&period;setText&lpar;item&period;bookName&rpar;<&sol;strong>&NewLine;<strong> imageView&period;setImageResource&lpar;item&period;imageResource&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;setting onclicklistener on each item<&sol;strong><&sol;span>&NewLine;<strong> itemView&period;setOnClickListener&lpar;&lbrace;<&sol;strong>&NewLine;&NewLine;<strong> Toast&period;makeText&lpar;itemView&period;context&comma; item&period;bookName &plus; " " &plus; " book is Clicked"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;<&sol;strong>&NewLine;<strong> &rcub;&rpar;<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<strong> override fun onCreateViewHolder&lpar;parent&colon; ViewGroup&comma; viewType&colon; Int&rpar;&colon; MyViewHolder &lbrace;<&sol;strong>&NewLine;<strong> val view &equals; LayoutInflater&period;from&lpar;parent&period;context&rpar;&period;inflate&lpar;R&period;layout&period;book&lowbar;list&lowbar;row&comma; parent&comma; false&rpar;<&sol;strong>&NewLine;<strong> return MyViewHolder&lpar;view&rpar;<&sol;strong>&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<strong> override fun onBindViewHolder&lpar;holder&colon; MyViewHolder&comma; position&colon; Int&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> holder&period;bindItems&lpar;bookList&period;get&lpar;position&rpar;&rpar;&semi;<&sol;strong>&NewLine;&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<strong> override fun getItemCount&lpar;&rpar;&colon; Int &lbrace;<&sol;strong>&NewLine;<strong> return bookList&period;size<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;&NewLine;<strong>&rcub;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong>7<&sol;strong>&period; Open drawable folder under res directory and paste all the book photos which you have copied from somewhere else&period;<&sol;p>&NewLine;<p><strong>8<&sol;strong>&period; Now open <span style&equals;"color&colon; &num;008000&semi;"><strong>BookListActivity&period;java<&sol;strong><&sol;span> refracted form of <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and do the below changes&period; Here <span style&equals;"color&colon; &num;0000ff&semi;"><strong>prepareMovieData&lpar;&rpar;<&sol;strong><&sol;span> method adds sample data to list view&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">BookListActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>package com&period;example&period;lenovo&period;recyclerviewkotlin<&sol;strong>&NewLine;&NewLine;<strong>import android&period;support&period;v7&period;app&period;AppCompatActivity<&sol;strong>&NewLine;<strong>import android&period;os&period;Bundle<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;DefaultItemAnimator<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;DividerItemDecoration<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;LinearLayoutManager<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;RecyclerView<&sol;strong>&NewLine;<strong>import java&period;util&period;ArrayList<&sol;strong>&NewLine;&NewLine;<strong>class BookListActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> private var recyclerView&colon; RecyclerView&quest; &equals; null<&sol;strong>&NewLine;<strong> private val bookList &equals; ArrayList&lt&semi;Book&gt&semi;&lpar;&rpar;<&sol;strong>&NewLine;<strong> private val mAdapter&colon; BookAdapter&quest; &equals; null<&sol;strong>&NewLine;&NewLine;<strong> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<&sol;strong>&NewLine;<strong> super&period;onCreate&lpar;savedInstanceState&rpar;<&sol;strong>&NewLine;<strong> setContentView&lpar;R&period;layout&period;booklist&rpar;<&sol;strong>&NewLine;<strong> val recyclerView&colon; RecyclerView &equals; findViewById&lpar;R&period;id&period;recycler&lowbar;view&rpar;<&sol;strong>&NewLine;&NewLine;<strong> prepareBookData&lpar;&rpar;<&sol;strong>&NewLine;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;vertical RecyclerView<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;layoutManager &equals; LinearLayoutManager&lpar;applicationContext&rpar;<&sol;strong>&NewLine;&NewLine; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;provides basic animations on remove&comma; add&comma; and move events that happen to the items in a RecyclerView&period;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;RecyclerView uses a DefaultItemAnimator by default&period;<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;itemAnimator &equals; DefaultItemAnimator&lpar;&rpar;<&sol;strong>&NewLine;&NewLine; &NewLine;<strong> val adapter &equals; BookAdapter&lpar;bookList&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;now adding the adapter to recyclerview<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;adapter &equals; adapter<&sol;strong>&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;preparing bookList data<&sol;strong><&sol;span>&NewLine;<strong> private fun prepareBookData&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;<strong> var book &equals; Book&lpar;"Munshi Premchand"&comma; "Nirmala"&comma; R&period;drawable&period;nirmala&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Harivansh Rai Bachchan"&comma; "Madhushala"&comma; R&period;drawable&period;madhushala2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Ramdhari Singh Dinkar"&comma; "Rashmirathi"&comma; R&period;drawable&period;rashmirathi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Dharamvir Bharti"&comma; "Gunahon Ka Devta"&comma; R&period;drawable&period;gunahokedevta2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Munshi Premchand"&comma; "Shatranj Ke Khiladi"&comma; R&period;drawable&period;shatranj2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Bhisham Sahni"&comma; "Tamas"&comma; R&period;drawable&period;tamas&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Dharamvir Bharti"&comma; "Suraj Ka Satva Ghoda"&comma; R&period;drawable&period;suraj&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Munshi Premchand"&comma; "Karmabhumi"&comma; R&period;drawable&period;karmabhumi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Kashinath Singh"&comma; "Kassi Ka Assi"&comma; R&period;drawable&period;kashi&lowbar;ka&lowbar;assi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Bhagwati Charan Verma"&comma; "Chitralekha"&comma; R&period;drawable&period;chitralekha&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<strong>&rcub;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Now if you run the app you can see the book list will look like this&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;" wp-image-1560 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1581062693&period;png" alt&equals;"Screenshot&lowbar;1581062693" width&equals;"366" height&equals;"650" &sol;><&sol;p>&NewLine;<p style&equals;"padding-left&colon; 30px&semi;">&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Adding RecyclerView Divider&sol;Separator<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>9&period;You can add the divider line between rows by using <span style&equals;"color&colon; &num;008000&semi;"><strong>DividerItemDecoration<&sol;strong><&sol;span> provided by the support library&period; Add item decoration on RecyclerView as shown below&period;<&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; adding inbuilt divider line<&sol;strong><&sol;span>&NewLine;<strong>recyclerView&excl;&excl;&period;addItemDecoration&lpar;DividerItemDecoration&lpar;this&comma; LinearLayoutManager&period;VERTICAL&rpar;&rpar;<&sol;strong>&NewLine;&NewLine;<strong>val adapter &equals; BookAdapter&lpar;bookList&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;now adding the adapter to recyclerview<&sol;strong><&sol;span>&NewLine;<strong>recyclerView&excl;&excl;&period;adapter &equals; adapter<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Now if you run the app&comma; you should see a divider line separating each row&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-107 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;02&sol;Screenshot&lowbar;2018-02-17-13-43-56-576x1024&period;png" alt&equals;"" width&equals;"375" height&equals;"667" &sol;><&sol;p>&NewLine;<p style&equals;"padding-left&colon; 30px&semi;">&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Displaying Horizontal Scrolling RecyclerView<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>10<&sol;strong>&period; If you want to display the RecyclerView in Horizontal manner&comma; you can do that just by changing a single line of code&period; All you have to do is provide direction to layout manager i&period;e <span style&equals;"color&colon; &num;008000&semi;"><strong>LinearLayoutManager&period;HORIZONTAL<&sol;strong><&sol;span> as shown below&period;<&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 60px&semi;"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;horizontal RecyclerView<&sol;strong><&sol;span>&NewLine;<strong>recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma;LinearLayoutManager&period;HORIZONTAL&comma;false&rpar;&rpar;&semi;<&sol;strong><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Final Code<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Below is the complete code of  <span style&equals;"color&colon; &num;008000&semi;"><strong>BookListActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">BookListActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong>package com&period;example&period;lenovo&period;recyclerviewkotlin<&sol;strong>&NewLine;&NewLine;<strong>import android&period;support&period;v7&period;app&period;AppCompatActivity<&sol;strong>&NewLine;<strong>import android&period;os&period;Bundle<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;DefaultItemAnimator<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;DividerItemDecoration<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;LinearLayoutManager<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;widget&period;RecyclerView<&sol;strong>&NewLine;<strong>import java&period;util&period;ArrayList<&sol;strong>&NewLine;&NewLine;<strong>class BookListActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> private var recyclerView&colon; RecyclerView&quest; &equals; null<&sol;strong>&NewLine;<strong> private val bookList &equals; ArrayList&lt&semi;Book&gt&semi;&lpar;&rpar;<&sol;strong>&NewLine;<strong> private val mAdapter&colon; BookAdapter&quest; &equals; null<&sol;strong>&NewLine;&NewLine;<strong> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<&sol;strong>&NewLine;<strong> super&period;onCreate&lpar;savedInstanceState&rpar;<&sol;strong>&NewLine;<strong> setContentView&lpar;R&period;layout&period;booklist&rpar;<&sol;strong>&NewLine;<strong> val recyclerView&colon; RecyclerView &equals; findViewById&lpar;R&period;id&period;recycler&lowbar;view&rpar;<&sol;strong>&NewLine;&NewLine;<strong> prepareBookData&lpar;&rpar;<&sol;strong>&NewLine;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;vertical RecyclerView<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;layoutManager &equals; LinearLayoutManager&lpar;applicationContext&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;horizontal RecyclerView<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma;LinearLayoutManager&period;HORIZONTAL&comma;false&rpar;&rpar;&semi;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;provides basic animations on remove&comma; add&comma; and move events that happen to the items in a RecyclerView&period;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;RecyclerView uses a DefaultItemAnimator by default&period;<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;itemAnimator &equals; DefaultItemAnimator&lpar;&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; adding inbuilt divider line<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;addItemDecoration&lpar;DividerItemDecoration&lpar;this&comma; LinearLayoutManager&period;VERTICAL&rpar;&rpar;<&sol;strong>&NewLine;&NewLine;<strong> val adapter &equals; BookAdapter&lpar;bookList&rpar;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;now adding the adapter to recyclerview<&sol;strong><&sol;span>&NewLine;<strong> recyclerView&excl;&excl;&period;adapter &equals; adapter<&sol;strong>&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;preparing bookList data<&sol;strong><&sol;span>&NewLine;<strong> private fun prepareBookData&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;<strong> var book &equals; Book&lpar;"Munshi Premchand"&comma; "Nirmala"&comma; R&period;drawable&period;nirmala&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Harivansh Rai Bachchan"&comma; "Madhushala"&comma; R&period;drawable&period;madhushala2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Ramdhari Singh Dinkar"&comma; "Rashmirathi"&comma; R&period;drawable&period;rashmirathi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Dharamvir Bharti"&comma; "Gunahon Ka Devta"&comma; R&period;drawable&period;gunahokedevta2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Munshi Premchand"&comma; "Shatranj Ke Khiladi"&comma; R&period;drawable&period;shatranj2&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Bhisham Sahni"&comma; "Tamas"&comma; R&period;drawable&period;tamas&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Dharamvir Bharti"&comma; "Suraj Ka Satva Ghoda"&comma; R&period;drawable&period;suraj&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Munshi Premchand"&comma; "Karmabhumi"&comma; R&period;drawable&period;karmabhumi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Kashinath Singh"&comma; "Kassi Ka Assi"&comma; R&period;drawable&period;kashi&lowbar;ka&lowbar;assi&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;<strong> book &equals; Book&lpar;"Bhagwati Charan Verma"&comma; "Chitralekha"&comma; R&period;drawable&period;chitralekha&rpar;<&sol;strong>&NewLine;<strong> bookList&period;add&lpar;book&rpar;<&sol;strong>&NewLine;&NewLine;<strong> &rcub;<&sol;strong>&NewLine;&NewLine;<strong>&rcub;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong>Now when you run the app&comma; it will look like this&period;<&sol;strong><&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-107 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;02&sol;Screenshot&lowbar;2018-02-17-13-43-56-576x1024&period;png" alt&equals;"" width&equals;"469" height&equals;"833" &sol;>&NewLine;

Exit mobile version