Site icon C1CTech

Android Data Binding In RecyclerView

<p class&equals;"p2">In the previous article&comma; we have learned about the <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-jetpack-data-binding&sol;">Basics of dataBinding<&sol;a>&period;<&sol;span><&sol;strong>Now In this android data binding RecyclerView tutorial&comma; I’m going to show how to implement data binding in Android <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&period; Data binding binds the UI with data sources and reduces lines of code&period;<&sol;p>&NewLine;<p>Using DataBinding in an adapter class keeps the code to very minimal as lot of things will be taken care in the layout itself&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Get GITHUB 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;AndroidDataBindingInRecyclerView">here<&sol;a><&sol;span>&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"5NN&lowbar;mM6ff3Q" title&equals;"Android DataBinding In RecyclerView"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;5NN&lowbar;mM6ff3Q"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;5NN&lowbar;mM6ff3Q&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android DataBinding In RecyclerView"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating New Project<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><strong>1<&sol;strong>&period; Create a new project in Android Studio from <span style&equals;"color&colon; &num;008000&semi;"><b>File <&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><b> New Project<&sol;b><&sol;span> and fill the project details&period;<&sol;p>&NewLine;<p class&equals;"p1"><b>2<&sol;b>&period; Enable DataBinding in <span style&equals;"color&colon; &num;008000&semi;"><b>app&sol;build&period;gradle<&sol;b><&sol;span>&period; Also add the <strong><span class&equals;"s2" style&equals;"color&colon; &num;0000ff&semi;">RecyclerView<&sol;span><&sol;strong>&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">CardView<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">CircleImageView<&sol;span><&sol;strong> dependencies and <span style&equals;"color&colon; &num;008000&semi;"><b>Sync<&sol;b><&sol;span> the project&period;<&sol;p>&NewLine;<pre>android &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">dataBinding &lbrace; <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> enabled &equals; true<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &rcub;<&sol;span><&sol;strong>&NewLine;&rcub;&NewLine;&NewLine;dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'androidx&period;appcompat&colon;appcompat&colon;1&period;1&period;0'&NewLine; implementation 'androidx&period;constraintlayout&colon;constraintlayout&colon;1&period;1&period;3'&NewLine; testImplementation 'junit&colon;junit&colon;4&period;12'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;recyclerView<&sol;span><&sol;strong>&NewLine; implementation 'androidx&period;recyclerview&colon;recyclerview&colon;1&period;0&period;0'&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;cardView<&sol;strong><&sol;span>&NewLine; implementation 'androidx&period;cardview&colon;cardview&colon;1&period;0&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;circleImageView<&sol;span><&sol;strong>&NewLine; implementation 'de&period;hdodenhof&colon;circleimageview&colon;3&period;1&period;0'&NewLine;&NewLine; androidTestImplementation 'androidx&period;test&colon;runner&colon;1&period;2&period;0'&NewLine; androidTestImplementation 'androidx&period;test&period;espresso&colon;espresso-core&colon;3&period;2&period;0'&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong>3<&sol;strong>&period; Add the below <span style&equals;"color&colon; &num;0000ff&semi;"><strong>string-array<&sol;strong><&sol;span> for name and email in your <strong><span style&equals;"color&colon; &num;008000&semi;">strings&period;xml<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>strings&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;string-array name&equals;"names"&gt&semi;&NewLine; &lt&semi;item&gt&semi;Arun Chandravanshi&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Pravesh Kumar&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Ranu Deshmuk&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Neelam Roy&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Nikita Desai&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Mohan Bhatiya&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Anikesh Danial&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Chris Danial &lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Koshiki Chandravanshi&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;Neha pandey&lt&semi;&sol;item&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;string-array&gt&semi;&NewLine;&NewLine;&lt&semi;string-array name&equals;"emails"&gt&semi;&NewLine;&NewLine; &lt&semi;item&gt&semi;arun11&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;pravesh12&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;ranu13&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;neelam14&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;nikita15&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;mohan16&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;anikesh17&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;chris18&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;koshiki19&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine; &lt&semi;item&gt&semi;neha20&commat;gmail&period;com&lt&semi;&sol;item&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;string-array&gt&semi;<&sol;pre>&NewLine;<p><strong>4<&sol;strong>&period; Open the layout file of main activity i&period;e <strong><span style&equals;"color&colon; &num;008000&semi;">activity&lowbar;main&period;xml<&sol;span>&period;<&sol;strong> Enable data-binding by adding <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;layout&gt&semi;<&sol;strong><&sol;span> tag and inside root view&comma; add <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;layout 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"&gt&semi;&NewLine;&NewLine; &lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&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;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;androidx&period;recyclerview&period;widget&period;RecyclerView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;recyclerView"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;scrollbars&equals;"vertical"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;padding&equals;"4dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;layout&gt&semi;<&sol;pre>&NewLine;<p><strong>5<&sol;strong>&period; Create a model class named <span style&equals;"color&colon; &num;008000&semi;"><strong>Employee&period;java<&sol;strong><&sol;span>&period; This class provides data to <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Employee&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;databindingdemo&semi;&NewLine;&NewLine;import android&period;view&period;View&semi;&NewLine;&NewLine;import androidx&period;core&period;content&period;ContextCompat&semi;&NewLine;import androidx&period;databinding&period;BindingAdapter&semi;&NewLine;import de&period;hdodenhof&period;circleimageview&period;CircleImageView&semi;&NewLine;&NewLine;public class Employee &lbrace;&NewLine;&NewLine; public String name&comma; email&semi;&NewLine; public int imageId&semi;&NewLine;&NewLine; public Employee&lpar;String name&comma; String email&comma; int imageId&rpar; &lbrace;&NewLine; this&period;name &equals; name&semi;&NewLine; this&period;email &equals; email&semi;&NewLine; this&period;imageId &equals; imageId&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;code for loading image<&sol;span><&sol;strong>&NewLine; &commat;BindingAdapter&lpar;"android&colon;imageUrl"&rpar;&NewLine; public static void loadImage&lpar;View view&comma; int imageId&rpar; &lbrace;&NewLine;&NewLine; CircleImageView imageView &equals; &lpar;CircleImageView&rpar; view&semi;&NewLine;&NewLine; imageView&period;setImageDrawable&lpar;ContextCompat&period;getDrawable&lpar;imageView&period;getContext&lpar;&rpar;&comma; imageId&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>In the above class&comma; we are adding one method name <span style&equals;"color&colon; &num;0000ff&semi;"><strong>loadImage&lpar;&rpar;<&sol;strong><&sol;span> which annotated with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;BindingAdapter&lpar;&OpenCurlyDoubleQuote;android&colon;imageUrl”&rpar;&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Using <span style&equals;"color&colon; &num;008000&semi;"><strong>BindingAdapters<&sol;strong><&sol;span> you can create custom attributes for your views&period;<&sol;p>&NewLine;<pre>&lt&semi;de&period;hdodenhof&period;circleimageview&period;CircleImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;emp&lowbar;image"&NewLine; android&colon;layout&lowbar;width&equals;"80dp"&NewLine; android&colon;layout&lowbar;height&equals;"80dp"&NewLine; app&colon;civ&lowbar;border&lowbar;width&equals;"2dp"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;imageUrl&equals;"&commat;&lbrace;employee&period;imageId&rcub;"<&sol;strong><&sol;span>&NewLine; android&colon;scaleType&equals;"centerCrop"&NewLine; app&colon;civ&lowbar;border&lowbar;color&equals;"&num;FF000000"&sol;&gt&semi;<&sol;pre>&NewLine;<p>Now the above custom attribute <strong><span style&equals;"color&colon; &num;008000&semi;">imageUrl<&sol;span><&sol;strong> defined in CircleImageView gets linked with the <span style&equals;"color&colon; &num;008000&semi;"><strong>loadImage&lpar;&rpar;<&sol;strong><&sol;span>&period; That means whenever this attribute will read for any employee object then loadImage&lpar;&rpar; will be called and it will set the drawable Image to ImageView&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>DataBinding In RecyclerView<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p2">Binding a RecyclerView layout is similar to normal binding except few changes in onCreateViewHolder and onBindViewHolder methods&period;<&sol;p>&NewLine;<p class&equals;"p2"><strong>6<&sol;strong>&period; Create layout named <span style&equals;"color&colon; &num;008000&semi;"><strong>employee&lowbar;list&lowbar;item<&sol;strong><b>&period;xml<&sol;b><&sol;span>&period; This layout contains one <span style&equals;"color&colon; &num;008000&semi;"><b>ImageView<&sol;b><&sol;span> and two <strong><span style&equals;"color&colon; &num;008000&semi;">TextViews<&sol;span><&sol;strong> &lpar;for name and email &rpar; to render employee data in RecyclerView&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">In this layout&comma; data binding is enabled by keeping the root element as <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;layout&gt&semi;<&sol;strong><&sol;span>&period; The <span style&equals;"color&colon; &num;008000&semi;"><b>Employee<&sol;b><&sol;span> model is bound to this layout using <span style&equals;"color&colon; &num;008000&semi;"><b>&lt&semi;variable&gt&semi;<&sol;b><&sol;span> tag&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">employee&lowbar;list&lowbar;item&period;xml<&sol;span><&sol;strong><&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;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&NewLine;&lt&semi;layout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;bind&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;card&lowbar;view&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&gt&semi;&NewLine;&NewLine; &lt&semi;data&gt&semi;&NewLine;&NewLine; &lt&semi;variable&NewLine; name&equals;"employee"&NewLine; type&equals;"com&period;example&period;databindingdemo&period;Employee" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;data&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;adjustViewBounds&equals;"true"&gt&semi;&NewLine;&NewLine; &lt&semi;androidx&period;cardview&period;widget&period;CardView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;cvEmployee"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;layout&lowbar;margin&equals;"5dp"&NewLine; android&colon;elevation&equals;"3dp"&NewLine; card&lowbar;view&colon;cardCornerRadius&equals;"1dp"&gt&semi;&NewLine;&NewLine; &lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&gt&semi;&NewLine;&NewLine; &lt&semi;de&period;hdodenhof&period;circleimageview&period;CircleImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;emp&lowbar;image"&NewLine; android&colon;layout&lowbar;width&equals;"80dp"&NewLine; android&colon;layout&lowbar;height&equals;"80dp"&NewLine; app&colon;civ&lowbar;border&lowbar;width&equals;"2dp"&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"8dp"&NewLine; android&colon;background&equals;"&quest;attr&sol;selectableItemBackgroundBorderless"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;imageUrl&equals;"&commat;&lbrace;employee&period;imageId&rcub;"<&sol;strong><&sol;span>&NewLine; android&colon;scaleType&equals;"centerCrop"&NewLine; bind&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; bind&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; bind&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;civ&lowbar;border&lowbar;color&equals;"&num;FF000000"&sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tvFullName"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;emp&lowbar;image"&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; android&colon;padding&equals;"4dp"&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;text&equals;"&commat;&lbrace;employee&period;name&rcub;"<&sol;strong><&sol;span>&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; bind&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; bind&colon;layout&lowbar;constraintStart&lowbar;toEndOf&equals;"&commat;&plus;id&sol;emp&lowbar;image"&NewLine; bind&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tvEmail"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;below&equals;"&commat;&plus;id&sol;tvFullName"&NewLine; android&colon;layout&lowbar;marginStart&equals;"8dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"4dp"&NewLine; android&colon;layout&lowbar;marginEnd&equals;"8dp"&NewLine; android&colon;padding&equals;"4dp"&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">android&colon;text&equals;"&commat;&lbrace;employee&period;email&rcub;"<&sol;span><&sol;strong>&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;textSize&equals;"16sp"&NewLine; bind&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; bind&colon;layout&lowbar;constraintStart&lowbar;toEndOf&equals;"&commat;&plus;id&sol;emp&lowbar;image"&NewLine; bind&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tvFullName" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;&NewLine; &lt&semi;&sol;androidx&period;cardview&period;widget&period;CardView&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;layout&gt&semi;<&sol;pre>&NewLine;<p class&equals;"p1"><strong>7<&sol;strong>&period; Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>EmployeeData<&sol;strong><b>Adapter&period;java<&sol;b> <&sol;span>under the <b>root<&sol;b> folder&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">As the layout name is <span style&equals;"color&colon; &num;008000&semi;"><strong>employee<&sol;strong><b>&lowbar;list&lowbar;item&period;xml<&sol;b><&sol;span>&comma; the generated binding class will be <span style&equals;"color&colon; &num;0000ff&semi;"><strong>EmployeeListItemBinding<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li class&equals;"li1">In <span style&equals;"color&colon; &num;0000ff&semi;"><b>onCreateViewHolder&lpar;&rpar;<&sol;b><&sol;span> method&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>employee<&sol;strong><b>&lowbar;list&lowbar;item<&sol;b><&sol;span> layout is inflated with the help of <span style&equals;"color&colon; &num;008000&semi;"><strong>EmployeeListItemBinding<&sol;strong><&sol;span> class&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><b>holder&period;binding&period;setEmployee&lpar;&rpar;<&sol;b><&sol;span> binds the <span style&equals;"color&colon; &num;008000&semi;"><b>Employee<&sol;b><&sol;span> model to each row&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">EmployeeDataAdapter&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;databindingdemo&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;&NewLine;import com&period;example&period;databindingdemo&period;databinding&period;EmployeeListItemBinding&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;import androidx&period;annotation&period;NonNull&semi;&NewLine;import androidx&period;databinding&period;DataBindingUtil&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;RecyclerView&semi;&NewLine;&NewLine;public class EmployeeDataAdapter extends RecyclerView&period;Adapter&lt&semi;EmployeeDataAdapter&period;EmployeeViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; Context context&semi;&NewLine; List&lt&semi;Employee&gt&semi; employeeList&semi;&NewLine;&NewLine; public EmployeeDataAdapter&lpar;Context context&comma; List&lt&semi;Employee&gt&semi; employeeList&rpar; &lbrace;&NewLine; this&period;context &equals; context&semi;&NewLine; this&period;employeeList &equals; employeeList&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;NonNull&NewLine; &commat;Override&NewLine; public EmployeeViewHolder onCreateViewHolder&lpar;&commat;NonNull ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine;&NewLine; EmployeeListItemBinding binding &equals; DataBindingUtil&period;inflate&lpar;LayoutInflater&period;from&lpar;parent&period;getContext&lpar;&rpar;&rpar;&comma; R&period;layout&period;employee&lowbar;list&lowbar;item&comma; parent&comma; false&rpar;&semi;&NewLine;&NewLine; return new EmployeeViewHolder&lpar;binding&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;&commat;NonNull EmployeeViewHolder holder&comma; int position&rpar; &lbrace;&NewLine;&NewLine; Employee employee &equals; employeeList&period;get&lpar;position&rpar;&semi;&NewLine;&NewLine; holder&period;binding&period;setEmployee&lpar;employee&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine; return employeeList&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; class EmployeeViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine;&NewLine; private EmployeeListItemBinding binding&semi;&NewLine;&NewLine; public EmployeeViewHolder&lpar;&commat;NonNull EmployeeListItemBinding binding&rpar; &lbrace;&NewLine; super&lpar;binding&period;getRoot&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; this&period;binding &equals; binding&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p2"><strong>8<&sol;strong>&period; Finally open <span style&equals;"color&colon; &num;008000&semi;"><b>MainActivity&period;java<&sol;b><&sol;span> and do the below modifications&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">As the main activity layout name is <span style&equals;"color&colon; &num;008000&semi;"><b>activity&lowbar;main<&sol;b><&sol;span>&comma; the generated binding class will be <span style&equals;"color&colon; &num;008000&semi;"><b>ActivityMainBinding<&sol;b><&sol;span>&period;<&sol;li>&NewLine;<li class&equals;"li2"><span style&equals;"color&colon; &num;008000&semi;"><b>prepareData&lpar;&rpar;<&sol;b><&sol;span> will prepare list of employees which we will pass to <span style&equals;"color&colon; &num;0000ff&semi;"><strong>EmployeeDataAdapter<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;databindingdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;&NewLine;import com&period;example&period;databindingdemo&period;databinding&period;ActivityMainBinding&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;Arrays&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;import androidx&period;databinding&period;DataBindingUtil&semi;&NewLine;import androidx&period;recyclerview&period;widget&period;LinearLayoutManager&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; ActivityMainBinding binding&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine;&NewLine;&NewLine; binding &equals; DataBindingUtil&period;setContentView&lpar;this&comma; R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine; binding&period;recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;this&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; binding&period;recyclerView&period;setAdapter&lpar;new EmployeeDataAdapter&lpar;this&comma; prepareData&lpar;&rpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; public List&lt&semi;Employee&gt&semi; prepareData&lpar;&rpar; &lbrace;&NewLine;&NewLine; List&lt&semi;String&gt&semi; names &equals; Arrays&period;asList&lpar;getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;names&rpar;&rpar;&semi;&NewLine; List&lt&semi;String&gt&semi; emails &equals; Arrays&period;asList&lpar;getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;emails&rpar;&rpar;&semi;&NewLine;&NewLine; int&lbrack;&rsqb; imageIds &equals; &lbrace;R&period;drawable&period;boy&lowbar;img2&comma; R&period;drawable&period;boy&lowbar;img1&comma; R&period;drawable&period;girl&lowbar;img1&comma; R&period;drawable&period;girl&lowbar;img2&comma; R&period;drawable&period;girl&lowbar;img3&comma; R&period;drawable&period;boy&lowbar;img3&comma; R&period;drawable&period;boy&lowbar;img5&comma; R&period;drawable&period;boy&lowbar;img4&comma; R&period;drawable&period;girl&lowbar;img5&comma; R&period;drawable&period;girl&lowbar;img4&rcub;&semi;&NewLine;&NewLine; List&lt&semi;Employee&gt&semi; employeeList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&NewLine; int count &equals; 0&semi;&NewLine;&NewLine; for &lpar;String name &colon; names&rpar; &lbrace;&NewLine; employeeList&period;add&lpar;new Employee&lpar;name&comma; emails&period;get&lpar;count&rpar;&comma; imageIds&lbrack;count&rsqb;&rpar;&rpar;&semi;&NewLine; count&plus;&plus;&semi;&NewLine; &rcub;&NewLine;&NewLine; return employeeList&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>When you run your application it will look like this&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1580" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1582208918&period;png" alt&equals;"Screenshot&lowbar;1582208918" width&equals;"459" height&equals;"816" &sol;><&sol;p>&NewLine;<p>In the next article&comma; we will talk about how to work with Observable data objects using dataBinding&period;&NewLine;

Exit mobile version