Site icon C1CTech

Android RecyclerView Example

<h3 style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;333399&semi;"><strong>RecyclerView<&sol;strong><&sol;span><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;">The<span style&equals;"color&colon; &num;3366ff&semi;"> <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span><&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 style&equals;"text-align&colon; left&semi;">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>Download <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;RecyclerViewExample"><strong>Here<&sol;strong><&sol;a>&period;<&sol;span><&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;youtu&period;be&sol;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 style&equals;"text-align&colon; left&semi;"><strong><span style&equals;"color&colon; &num;333399&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;000000&semi;"><strong>1&period; <&sol;strong><&sol;span> 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 style&equals;"text-align&colon; left&semi;"><strong>2&period;  <&sol;strong>Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> and add recycler view dependency <span style&equals;"color&colon; &num;0000ff&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 style&equals;"text-align&colon; left&semi;"><strong>Build&period;gradle<&sol;strong><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;<strong>RecyclerView<&sol;strong><&sol;span>&NewLine; implementation <span style&equals;"color&colon; &num;0000ff&semi;"><strong>'com&period;android&period;support&colon;recyclerview-v7&colon;26&period;1&period;0'<&sol;strong><&sol;span>&NewLine; &NewLine;&rcub;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>3&period; <&sol;strong>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 style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;booklist&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"com&period;example&period;lenovo&period;recyclerview&period;activity&period;BookListActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;RecyclerView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;recycler&lowbar;view"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;scrollbars&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;v7&period;widget&period;RecyclerView&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;333399&semi;"><strong>Writing the Adapter Class<&sol;strong><&sol;span><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;">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 style&equals;"text-align&colon; left&semi;"><strong>4&period; <&sol;strong>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; Also add the getter&sol;setter methods to each variable&period;<&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Book&period;java <&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;recyclerview&period;model&semi;&NewLine;&NewLine;public class Book &lbrace;&NewLine; String authorName&semi;&NewLine; String bookName&semi;&NewLine; int imageResource&semi;&NewLine;&NewLine; public Book&lpar;String authorName&comma; String bookName&comma; int imageResource&rpar; &lbrace;&NewLine; this&period;authorName &equals; authorName&semi;&NewLine; this&period;bookName &equals; bookName&semi;&NewLine; this&period;imageResource &equals; imageResource&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getAuthorName&lpar;&rpar; &lbrace;&NewLine; return authorName&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setAuthorName&lpar;String authorName&rpar; &lbrace;&NewLine; this&period;authorName &equals; authorName&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getBookName&lpar;&rpar; &lbrace;&NewLine; return bookName&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setBookName&lpar;String bookName&rpar; &lbrace;&NewLine; this&period;bookName &equals; bookName&semi;&NewLine; &rcub;&NewLine;&NewLine; public int getImageResource&lpar;&rpar; &lbrace;&NewLine; return imageResource&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setImageResource&lpar;int imageResource&rpar; &lbrace;&NewLine; this&period;imageResource &equals; imageResource&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>5&period; <&sol;strong>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<strong><span style&equals;"color&colon; &num;008000&semi;"> name&comma; image&comma; and author name<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>book&lowbar;list&lowbar;row&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;margin&equals;"5dp"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;img&lowbar;book"&NewLine; android&colon;layout&lowbar;width&equals;"100dp"&NewLine; android&colon;layout&lowbar;height&equals;"200dp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"10dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;book&lowbar;name"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;textAllCaps&equals;"true"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"22sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;author&lowbar;name"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>6&period; <&sol;strong>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; book name&comma; and book image&rpar; set to each row&period;<&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>BookAdapter&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;recyclerview&period;adapter&semi;&NewLine;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;model&period;Book&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class BookAdapter extends RecyclerView&period;Adapter&lt&semi;BookAdapter&period;MyViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; private List&lt&semi;Book&gt&semi; bookList&semi;&NewLine;&NewLine; public BookAdapter&lpar;List&lt&semi;Book&gt&semi; bookList&rpar; &lbrace;&NewLine; this&period;bookList &equals; bookList&semi;&NewLine; &rcub;&NewLine;&NewLine; public class MyViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine; public TextView authorName&comma; bookName&semi;&NewLine; public ImageView imageView&semi;&NewLine;&NewLine; public MyViewHolder&lpar;View view&rpar; &lbrace;&NewLine; super&lpar;view&rpar;&semi;&NewLine; authorName &equals; &lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;author&lowbar;name&rpar;&semi;&NewLine; bookName &equals; &lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;book&lowbar;name&rpar;&semi;&NewLine; imageView &equals; &lpar;ImageView&rpar; view&period;findViewById&lpar;R&period;id&period;img&lowbar;book&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public MyViewHolder onCreateViewHolder&lpar;ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine; View itemView &equals; LayoutInflater&period;from&lpar;parent&period;getContext&lpar;&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;book&lowbar;list&lowbar;row&comma; parent&comma; false&rpar;&semi;&NewLine; return new MyViewHolder&lpar;itemView&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;MyViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; holder&period;authorName&period;setText&lpar;bookList&period;get&lpar;position&rpar;&period;getAuthorName&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;bookName&period;setText&lpar;bookList&period;get&lpar;position&rpar;&period;getBookName&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;imageView&period;setImageResource&lpar;bookList&period;get&lpar;position&rpar;&period;getImageResource&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine; return bookList&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>7&period; <&sol;strong>Open drawable folder under res directory and paste all the book photos which you have copied from somewhere else&period;<&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>8&period; <&sol;strong>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 style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>BookListActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;recyclerview&period;activity&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DefaultItemAnimator&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DividerItemDecoration&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;MotionEvent&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;adapter&period;BookAdapter&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;model&period;Book&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class BookListActivity extends AppCompatActivity &lbrace;&NewLine; private RecyclerView recyclerView&semi;&NewLine; private List&lt&semi;Book&gt&semi; bookList &equals; new ArrayList&lt&semi;Book&gt&semi;&lpar;&rpar;&semi;&NewLine; private BookAdapter mAdapter&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;booklist&rpar;&semi;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;recycler&lowbar;view&rpar;&semi;&NewLine;&NewLine; prepareBookData&lpar;&rpar;&semi;&NewLine;&NewLine; mAdapter &equals; new BookAdapter&lpar;bookList&rpar;&semi;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;vertical RecyclerView<&sol;span><&sol;strong>&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&rpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;provides basic animations on remove&comma; add&comma; and move events that happen to the items in a RecyclerView&period;&NewLine; &sol;&sol;RecyclerView uses a DefaultItemAnimator by default&period;<&sol;span><&sol;strong>&NewLine; recyclerView&period;setItemAnimator&lpar;new DefaultItemAnimator&lpar;&rpar;&rpar;&semi;&NewLine; recyclerView&period;setAdapter&lpar;mAdapter&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;preparing bookList data<&sol;span><&sol;strong>&NewLine; private void prepareBookData&lpar;&rpar; &lbrace;&NewLine; Book book &equals; new Book&lpar;"Munshi Premchand"&comma; "Nirmala"&comma; R&period;drawable&period;nirmala&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Harivansh Rai Bachchan"&comma; "Madhushala"&comma; R&period;drawable&period;madhushala2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Ramdhari Singh Dinkar"&comma; "Rashmirathi"&comma; R&period;drawable&period;rashmirathi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Dharamvir Bharti"&comma; "Gunahon Ka Devta"&comma; R&period;drawable&period;gunahokedevta2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Munshi Premchand"&comma; "Shatranj Ke Khiladi"&comma; R&period;drawable&period;shatranj2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Bhisham Sahni"&comma; "Tamas"&comma; R&period;drawable&period;tamas&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Dharamvir Bharti"&comma; "Suraj Ka Satva Ghoda"&comma; R&period;drawable&period;suraj&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Munshi Premchand"&comma; "Karmabhumi"&comma; R&period;drawable&period;karmabhumi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Kashinath Singh"&comma; "Kassi Ka Assi"&comma; R&period;drawable&period;kashi&lowbar;ka&lowbar;assi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Bhagwati Charan Verma"&comma; "Chitralekha"&comma; R&period;drawable&period;chitralekha&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><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 style&equals;"text-align&colon; left&semi;"><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;"355" height&equals;"632" &sol;><&sol;p>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><strong><span style&equals;"color&colon; &num;333399&semi;">Adding RecyclerView Divider&sol;Separator<&sol;span><&sol;strong><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>9&period; <&sol;strong>You can add the divider line between rows by using<span style&equals;"color&colon; &num;0000ff&semi;"> <strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;support&sol;v7&sol;widget&sol;DividerItemDecoration&period;html">DividerItemDecoration<&sol;a><&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;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;000000&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; adding inbuilt divider line<&sol;span><&sol;strong><em>&NewLine;<&sol;em><strong>recyclerView<&sol;strong>&period;addItemDecoration&lpar;<strong>new <&sol;strong>DividerItemDecoration&lpar;<strong>this<&sol;strong>&comma; LinearLayoutManager&period;<strong><em>VERTICAL<&sol;em><&sol;strong>&rpar;&rpar;&semi;<&sol;span><&sol;pre>&NewLine;<pre style&equals;"text-align&colon; left&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;setting Adapter<&sol;span><&sol;strong>&NewLine;<span style&equals;"color&colon; &num;000000&semi;"> <strong>recyclerView<&sol;strong>&period;setAdapter&lpar;<strong>mAdapter<&sol;strong>&rpar;&semi;<&sol;span><&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><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;" wp-image-107 aligncenter" 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;"362" height&equals;"644" &sol;><&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;">&NewLine;<h2><&sol;h2>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><strong><span style&equals;"color&colon; &num;333399&semi;">Displaying Horizontal Scrolling RecyclerView<&sol;span><&sol;strong><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;"><strong>10&period; <&sol;strong>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;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;000000&semi;"><em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;horizontal recyclerView<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>recyclerView<&sol;strong>&period;setLayoutManager&lpar;<strong>new <&sol;strong>LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma;LinearLayoutManager&period;<strong><em>HORIZONTAL<&sol;em><&sol;strong>&comma;<strong>false<&sol;strong>&rpar;&rpar;&semi;<&sol;span><&sol;pre>&NewLine;<h3 style&equals;"text-align&colon; left&semi;"><strong><span style&equals;"color&colon; &num;333399&semi;">Complete Code<&sol;span><&sol;strong><&sol;h3>&NewLine;<p style&equals;"text-align&colon; left&semi;">Below is the complete code of  <span style&equals;"color&colon; &num;008000&semi;"><strong>BookListActivity&period;java&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p style&equals;"text-align&colon; left&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>BookListActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;recyclerview&period;activity&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DefaultItemAnimator&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DividerItemDecoration&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;MotionEvent&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;adapter&period;BookAdapter&semi;&NewLine;import com&period;example&period;lenovo&period;recyclerview&period;model&period;Book&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class BookListActivity extends AppCompatActivity &lbrace;&NewLine; private RecyclerView recyclerView&semi;&NewLine; private List&lt&semi;Book&gt&semi; bookList &equals; new ArrayList&lt&semi;Book&gt&semi;&lpar;&rpar;&semi;&NewLine; private BookAdapter mAdapter&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;booklist&rpar;&semi;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;recycler&lowbar;view&rpar;&semi;&NewLine;&NewLine; prepareBookData&lpar;&rpar;&semi;&NewLine;&NewLine; mAdapter &equals; new BookAdapter&lpar;bookList&rpar;&semi;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;vertical RecyclerView<&sol;strong><&sol;span>&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&rpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;horizontal RecyclerView<&sol;span><&sol;strong>&NewLine; &sol;&sol;recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma;LinearLayoutManager&period;HORIZONTAL&comma;false&rpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;provides basic animations on remove&comma; add&comma; and move events that happen to the items in a RecyclerView&period;&NewLine; &sol;&sol;RecyclerView uses a DefaultItemAnimator by default&period;<&sol;span><&sol;strong>&NewLine; recyclerView&period;setItemAnimator&lpar;new DefaultItemAnimator&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;adding inbuilt divider line<&sol;span><&sol;strong>&NewLine; recyclerView&period;addItemDecoration&lpar;new DividerItemDecoration&lpar;this&comma; LinearLayoutManager&period;VERTICAL&rpar;&rpar;&semi;&NewLine; recyclerView&period;setAdapter&lpar;mAdapter&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;preparing bookList data<&sol;strong><&sol;span>&NewLine; private void prepareBookData&lpar;&rpar; &lbrace;&NewLine; Book book &equals; new Book&lpar;"Munshi Premchand"&comma; "Nirmala"&comma; R&period;drawable&period;nirmala&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Harivansh Rai Bachchan"&comma; "Madhushala"&comma; R&period;drawable&period;madhushala2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Ramdhari Singh Dinkar"&comma; "Rashmirathi"&comma; R&period;drawable&period;rashmirathi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Dharamvir Bharti"&comma; "Gunahon Ka Devta"&comma; R&period;drawable&period;gunahokedevta2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Munshi Premchand"&comma; "Shatranj Ke Khiladi"&comma; R&period;drawable&period;shatranj2&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Bhisham Sahni"&comma; "Tamas"&comma; R&period;drawable&period;tamas&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Dharamvir Bharti"&comma; "Suraj Ka Satva Ghoda"&comma; R&period;drawable&period;suraj&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Munshi Premchand"&comma; "Karmabhumi"&comma; R&period;drawable&period;karmabhumi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Kashinath Singh"&comma; "Kassi Ka Assi"&comma; R&period;drawable&period;kashi&lowbar;ka&lowbar;assi&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine; book &equals; new Book&lpar;"Bhagwati Charan Verma"&comma; "Chitralekha"&comma; R&period;drawable&period;chitralekha&rpar;&semi;&NewLine; bookList&period;add&lpar;book&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p style&equals;"text-align&colon; left&semi;"><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-107 aligncenter" 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;"424" height&equals;"754" &sol;>&NewLine;

Exit mobile version