Site icon C1CTech

Android Jetpack Paging Example Using Room

<h3><span style&equals;"color&colon; &num;000080&semi;">Paging Library<&sol;span><&sol;h3>&NewLine;<p>Announced with new additions at Google IO 2018&comma; the Paging Library is a part of <span style&equals;"color&colon; &num;008000&semi;"><strong>Android<&sol;strong> <strong>Jetpack<&sol;strong><&sol;span>&period; The Paging Library makes it easier for you to load data gradually and gracefully within your app&&num;8217&semi;s <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>The Paging Library helps your app observe and display a reasonable subset of this data&period; This functionality has several advantages&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Data requests consume less network bandwidth and fewer system resources&period; Users who have metered or small data plans appreciate such data-conscious apps&period;<&sol;li>&NewLine;<li>Even during data updates and refreshes&comma; the app continues to respond quickly to user input&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Get <span style&equals;"color&colon; &num;000080&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;PagingExampleUsingRoom"><strong><span style&equals;"color&colon; &num;00ff00&semi;">HERE<&sol;span><&sol;strong><&sol;a>&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"m9K6inkyXPo" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;m9K6inkyXPo" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;m9K6inkyXPo&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;m9K6inkyXPo&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3 id&equals;"toc&lowbar;2"><span style&equals;"color&colon; &num;000080&semi;">Major Components<&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">DataSource<&sol;span><&sol;strong><&sol;p>&NewLine;<p>DataSource holds the content from the database and external sources such as network APIs and serves as a local storage for PagedLists&period; A PagedList is generated by LivePagedListBuilder with DataSourceFactory passed into it&comma; optionally with configuration parameters&period; The PagedList will contain a duplication of the DataSource&period; This process limits a PagedList to hold only a snapshot of data but allows multiple PagedLists from a single DataSource&period;<&sol;p>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">Room persistence library<&sol;span><&sol;strong> provides native support for dataSource associated with the Paging library&period; For a given query&comma; Room allows you to return a <strong><span style&equals;"color&colon; &num;008000&semi;">DataSource&period;Factory<&sol;span><&sol;strong> from the DAO and handles the implementation of the <span style&equals;"color&colon; &num;008000&semi;"><strong>DataSource<&sol;strong><&sol;span> for you&period; For example &colon;<&sol;p>&NewLine;<pre><code>&commat;Query&lpar;"SELECT &ast; FROM COUNTRIES"&rpar;&NewLine;public abstract DataSource&period;Factory&lt&semi;Integer&comma;Country&gt&semi; getCountries&lpar;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">PagedList<&sol;span><&sol;strong><&sol;p>&NewLine;<p>PagedList is the key component of the library&period; This component allows a RecyclerView to load a chunk of data from a DataSource&period;<&sol;p>&NewLine;<p>PagedList is a collection that loads data in pages&comma; asynchronously&period; A <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> can be used to load data from sources you define&comma; and present it easily in your UI with a <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>PagedListAdapter<&sol;strong><&sol;span><&sol;p>&NewLine;<p>PagedListAdapter is a <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView&period;Adapter <&sol;span><&sol;strong>that presents paged data from PagedLists in a RecyclerView&period; <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong> listens to PagedList loading callbacks as pages are loaded&comma; and uses <strong><span style&equals;"color&colon; &num;008000&semi;">DiffUtil<&sol;span><&sol;strong> to compute fine grained updates as new PagedLists are received and then display the list to user in your UI with a <strong><span style&equals;"color&colon; &num;008000&semi;">recyclerView<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">1&period; Adding Components to your Project<&sol;span><&sol;h3>&NewLine;<p id&equals;"add&lowbar;architecture&lowbar;components"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Add Architecture Components<&sol;span><&sol;strong><&sol;p>&NewLine;<p>To add Architechture Components in your app &comma; open app&&num;8217&semi;s <strong><span style&equals;"color&colon; &num;008000&semi;">build&period;gradle<&sol;span><&sol;strong> and add the following dependencies &colon;<&sol;p>&NewLine;<pre><code><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; ViewModel and LiveData<&sol;strong><&sol;span>&NewLine;implementation "android&period;arch&period;lifecycle&colon;extensions&colon;1&period;0&period;0"&NewLine;annotationProcessor "android&period;arch&period;lifecycle&colon;compiler&colon;1&period;0&period;0"&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Room<&sol;span><&sol;strong>&NewLine;implementation "android&period;arch&period;persistence&period;room&colon;runtime&colon;1&period;0&period;0"&NewLine;annotationProcessor "android&period;arch&period;persistence&period;room&colon;compiler&colon;1&period;0&period;0"&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&NewLine;&sol;&sol; Paging<&sol;strong><&sol;span>&NewLine;implementation "android&period;arch&period;paging&colon;runtime&colon;1&period;0&period;0-alpha4-1"<&sol;code><code>&NewLine;<&sol;code><&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">2&period; Create DataSource<&sol;span><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Create Entity<&sol;span><&sol;strong><&sol;p>&NewLine;<p>Represents a modal class whose objects we are going to store in our database&period; For each entity&comma; a database table is created to hold the items&period; You must reference the entity class in the <strong><span style&equals;"color&colon; &num;008000&semi;">Database<&sol;span><&sol;strong> class&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Country&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&commat;Entity&lpar;tableName &equals; TABLE&lowbar;NAME&rpar;&NewLine;public class Country &lbrace;&NewLine;&NewLine; public static final String TABLE&lowbar;NAME &equals; "COUNTRIES"&semi;&NewLine;&NewLine; public static DiffCallback&lt&semi;Country&gt&semi; DIFF&lowbar;CALLBACK &equals; new DiffCallback&lt&semi;Country&gt&semi;&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public boolean areItemsTheSame&lpar;&commat;NonNull Country oldItem&comma; &commat;NonNull Country newItem&rpar; &lbrace;&NewLine; return oldItem&period;id &equals;&equals; newItem&period;id&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean areContentsTheSame&lpar;&commat;NonNull Country oldItem&comma; &commat;NonNull Country newItem&rpar; &lbrace;&NewLine; return oldItem&period;equals&lpar;newItem&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&semi;&NewLine;&NewLine;&NewLine; &commat;PrimaryKey&lpar;autoGenerate &equals; true&rpar;&NewLine; public int id&semi;&NewLine;&NewLine; public String countryName&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public String toString&lpar;&rpar; &lbrace;&NewLine; return "Country&lbrace;" &plus;&NewLine; "id&equals;" &plus; id &plus;&NewLine; "&comma; countryName&equals;'" &plus; countryName &plus; '&bsol;'' &plus;&NewLine; '&rcub;'&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean equals&lpar;Object obj&rpar; &lbrace;&NewLine; if &lpar;obj &equals;&equals; this&rpar;&NewLine; return true&semi;&NewLine;&NewLine; Country country &equals; &lpar;Country&rpar; obj&semi;&NewLine;&NewLine; return country&period;id &equals;&equals; this&period;id &amp&semi;&amp&semi; country&period;countryName &equals;&equals; this&period;countryName&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Create Dao&lpar;<&sol;strong><&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Data Access Objects<&sol;strong><&sol;span><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&rpar;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">DataSource&period;Factory<&sol;span><&sol;strong> &lpar;implemented by Room&rpar; creates the <span style&equals;"color&colon; &num;008000&semi;"><strong>DataSource<&sol;strong><&sol;span>&period; Then&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>LivePagedListBuilder<&sol;strong><&sol;span> builds the <strong><span style&equals;"color&colon; &num;008000&semi;">LiveData&lt&semi;PagedList&gt&semi;<&sol;span><&sol;strong>&comma; using the passed-in <strong><span style&equals;"color&colon; &num;008000&semi;">DataSource&period;Factory<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> configuration&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre><code>&commat;Dao&NewLine;public interface CountryDao &lbrace;&NewLine;&NewLine; &commat;Insert&lpar;onConflict &equals; OnConflictStrategy&period;REPLACE&rpar;&NewLine; public void insertCountriesList&lpar;List&lt&semi;Country&gt&semi; countries&rpar;&semi;&NewLine;&NewLine; &commat;Query&lpar;"SELECT &ast; FROM COUNTRIES"&rpar;&NewLine; public abstract DataSource&period;Factory&lt&semi;Integer&comma;Country&gt&semi; getCountries&lpar;&rpar;&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Create Database<&sol;span><&sol;strong><&sol;p>&NewLine;<p>By calling <span style&equals;"color&colon; &num;008000&semi;"><strong>getCountryDatabase&lpar;&rpar;<&sol;strong><&sol;span> we get roomDatabase object using which we can get  <strong><span style&equals;"color&colon; &num;008000&semi;">CountryDao<&sol;span><&sol;strong>  instance to interact or query with database &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;<pre><code>&commat;Database&lpar;entities &equals; &lbrace;Country&period;class&rcub;&comma; version &equals; 1&rpar;&NewLine;public abstract class CountryDatabase extends RoomDatabase &lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Country&lowbar;Database&period;db"&semi;&NewLine; private static CountryDatabase INSTANCE&semi;&NewLine;&NewLine;&NewLine; public static CountryDatabase getCountryDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; CountryDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine; public static void destroyInstance&lpar;&rpar; &lbrace;&NewLine; INSTANCE &equals; null&semi;&NewLine; &rcub;&NewLine;&NewLine; public abstract CountryDao countryDao&lpar;&rpar;&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">3&period; Create ViewModel<&sol;span><&sol;h3>&NewLine;<p><code>LivePagedListBuilder<&sol;code>builds the <code>LiveData&lt&semi;PagedList&gt&semi;<&sol;code>&comma; using the passed-in <code>DataSource&period;Factory<&sol;code>and <code>PagedList<&sol;code> configuration&period;<&sol;p>&NewLine;<p>This <span style&equals;"color&colon; &num;008000&semi;"><strong>LivePagedListBuilder<&sol;strong><&sol;span> object is responsible for creating <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> objects&period; When a PagedList is created the <strong><span style&equals;"color&colon; &num;008000&semi;">LiveData<&sol;span><&sol;strong> emits the new <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> to the <span style&equals;"color&colon; &num;008000&semi;"><strong>ViewModel<&sol;strong><&sol;span>&comma; which in turn passes it to the <strong><span style&equals;"color&colon; &num;008000&semi;">UI<&sol;span><&sol;strong>&period; The UI observes the changed PagedList and uses its <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong> to update the <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView<&sol;span><&sol;strong> that presents the <span style&equals;"color&colon; &num;000000&semi;">PagedList<&sol;span> data&period;<&sol;p>&NewLine;<p>To build and configure a <span style&equals;"color&colon; &num;008000&semi;"><strong>LiveData&lt&semi;PagedList&gt&semi;<&sol;strong><&sol;span>&comma; use a <span style&equals;"color&colon; &num;008000&semi;"><strong>LivePagedListBuilder<&sol;strong><&sol;span>&period; Besides the <span style&equals;"color&colon; &num;008000&semi;"><strong>DataSource&period;Factory<&sol;strong><&sol;span>&comma; you need to provide a <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> configuration&comma; which can include the following options&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>setPageSize&lpar;int&rpar; &colon; the size of a page loaded by a <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong><&sol;li>&NewLine;<li>setPrefetchDistance&lpar;int&rpar; &colon; how far ahead to load<&sol;li>&NewLine;<li>setInitialLoadSizeHint&lpar;int&rpar; &colon; how many items to load when the first load occurs<&sol;li>&NewLine;<li>setEnablePlaceholders&lpar;boolean&rpar; &colon; whether null items to be added to the <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong>&comma; to represent data that hasn&&num;8217&semi;t been loaded yet&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre><code>public class CountryViewModel extends AndroidViewModel &lbrace;&NewLine;&NewLine; private CountryDatabase countryDatabase&semi;&NewLine;&NewLine; public CountryDao countryDao&semi;&NewLine;&NewLine; public LiveData&lt&semi;PagedList&lt&semi;Country&gt&semi;&gt&semi; countriesList&semi;&NewLine;&NewLine; public CountryViewModel&lpar;&commat;NonNull Application application&rpar; &lbrace;&NewLine; super&lpar;application&rpar;&semi;&NewLine;&NewLine; countryDatabase &equals; CountryDatabase&period;getCountryDatabase&lpar;this&period;getApplication&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; countryDao &equals; countryDatabase&period;countryDao&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public void init&lpar;CountryDao countryDao&rpar; &lbrace;&NewLine; PagedList&period;Config pagedListConfig &equals;&NewLine; &lpar;new PagedList&period;Config&period;Builder&lpar;&rpar;&rpar;&period;setEnablePlaceholders&lpar;true&rpar;&NewLine; &period;setPrefetchDistance&lpar;10&rpar;&NewLine; &period;setPageSize&lpar;20&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; countriesList &equals; &lpar;new LivePagedListBuilder&lpar;countryDao&period;getCountries&lpar;&rpar;&NewLine; &comma; pagedListConfig&rpar;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>In the onCreate&comma; we create the instance of <strong><span style&equals;"color&colon; &num;008000&semi;">ViewModel<&sol;span><&sol;strong>&period; In ViewModel class <strong><span style&equals;"color&colon; &num;008000&semi;">LiveData<&sol;span><&sol;strong> emits the new PagedList to the <strong><span style&equals;"color&colon; &num;008000&semi;">ViewModel<&sol;span><&sol;strong>&period; which in turn passes it to the <strong><span style&equals;"color&colon; &num;008000&semi;">UI<&sol;span><&sol;strong>&period; The UI observes the changed PagedList and uses its <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong> to update the <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView<&sol;span><&sol;strong> that presents the PagedList data&period;<&sol;p>&NewLine;<div id&equals;"crayon-5b3df81a4baa1738139756" class&equals;"crayon-syntax crayon-theme-github crayon-font-courier-new crayon-os-pc print-yes notranslate" data-settings&equals;" minimize scroll-mouseover">&NewLine;<div class&equals;"crayon-plain-wrap"><&sol;div>&NewLine;<div class&equals;"crayon-main">&NewLine;<pre><code>&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;main&rpar;&semi;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;countryList&rpar;&semi;&NewLine;&NewLine;&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma; LinearLayoutManager&period;VERTICAL&comma; false&rpar;&rpar;&semi;&NewLine;&NewLine; viewModel &equals; ViewModelProviders&period;of&lpar;this&rpar;&period;get&lpar;CountryViewModel&period;class&rpar;&semi;&NewLine; insertUser&lpar;viewModel&period;getCountryDatabase&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; viewModel&period;init&lpar;viewModel&period;countryDao&rpar;&semi;&NewLine;&NewLine; CountryAdapter countryAdapter &equals; new CountryAdapter&lpar;&rpar;&semi;&NewLine;&NewLine; viewModel&period;countriesList&period;observe&lpar;this&comma; pageList -&gt&semi; &lbrace;&NewLine; countryAdapter&period;setList&lpar;pageList&rpar;&semi;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setAdapter&lpar;countryAdapter&rpar;&semi;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">4&period; Create Adapter<&sol;span><&sol;h3>&NewLine;<p>To bind a <strong><span style&equals;"color&colon; &num;008000&semi;">PagedList<&sol;span><&sol;strong> to a <span style&equals;"color&colon; &num;008000&semi;"><strong>RecyclerView<&sol;strong><&sol;span>&comma; use a <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong>&period; The <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong> gets notified whenever the PagedList content is loaded and then signals the <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView<&sol;span><&sol;strong> to update&period;<&sol;p>&NewLine;<p>To tell the PagedListAdapter how to compute the difference between the two elements&comma; you’ll need to implement a new class&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">DiffCallback<&sol;span> <&sol;strong>&period;Here&comma; you will define two things as given below &colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">areItemsTheSame<&sol;span><&sol;strong> &colon;Checks whether both are reffering to the same item or not&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">areContentsTheSame<&sol;span><&sol;strong> &colon; Checks whether the content of both item are same or not&period;<&sol;p>&NewLine;<pre><code>public static DiffCallback&lt&semi;Country&gt&semi; DIFF&lowbar;CALLBACK &equals; new DiffCallback&lt&semi;Country&gt&semi;&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public boolean areItemsTheSame&lpar;&commat;NonNull Country oldItem&comma; &commat;NonNull Country newItem&rpar; &lbrace;&NewLine; return oldItem&period;id &equals;&equals; newItem&period;id&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean areContentsTheSame&lpar;&commat;NonNull Country oldItem&comma; &commat;NonNull Country newItem&rpar; &lbrace;&NewLine; return oldItem&period;equals&lpar;newItem&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&semi;&NewLine;&NewLine;&commat;Override&NewLine;public boolean equals&lpar;Object obj&rpar; &lbrace;&NewLine; if &lpar;obj &equals;&equals; this&rpar;&NewLine; return true&semi;&NewLine;&NewLine; Country country &equals; &lpar;Country&rpar; obj&semi;&NewLine;&NewLine; return country&period;id &equals;&equals; this&period;id &amp&semi;&amp&semi; country&period;countryName &equals;&equals; this&period;countryName&semi;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>Let’s look at the adapter&period;So our adapter would extend <strong><span style&equals;"color&colon; &num;008000&semi;">PagedListAdapter<&sol;span><&sol;strong>&comma; Here We define the callback&comma; the <strong><span style&equals;"color&colon; &num;008000&semi;">DIFF&lowbar;CALLBACK<&sol;span><&sol;strong>&comma; for our country objects and then  <span style&equals;"color&colon; &num;008000&semi;"><strong>onBindViewHolder&lpar;&rpar;<&sol;strong><&sol;span> binds the PagedList items to the ViewHolder one by one&period;<&sol;p>&NewLine;<pre><code>public class CountryAdapter extends PagedListAdapter&lt&semi;Country&comma; CountryAdapter&period;CountryViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; public CountryAdapter&lpar;&rpar; &lbrace;&NewLine; super&lpar;Country&period;DIFF&lowbar;CALLBACK&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;NonNull&NewLine; &commat;Override&NewLine; public CountryViewHolder onCreateViewHolder&lpar;&commat;NonNull ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine; View v &equals; LayoutInflater&period;from&lpar;parent&period;getContext&lpar;&rpar;&rpar;&NewLine; &period;inflate&lpar;R&period;layout&period;item&lowbar;country&lowbar;list&comma; parent&comma; false&rpar;&semi;&NewLine;&NewLine; return new CountryViewHolder&lpar;v&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;&commat;NonNull CountryViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; Country country &equals; getItem&lpar;position&rpar;&semi;&NewLine; if &lpar;country &excl;&equals; null&rpar; &lbrace;&NewLine; holder&period;bindTo&lpar;country&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; public static class CountryViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine;&NewLine; TextView countryName&semi;&NewLine; TextView uniqueId&semi;&NewLine;&NewLine;&NewLine; public CountryViewHolder&lpar;View itemView&rpar; &lbrace;&NewLine; super&lpar;itemView&rpar;&semi;&NewLine;&NewLine; countryName &equals; &lpar;TextView&rpar; itemView&period;findViewById&lpar;R&period;id&period;tv&lowbar;country&rpar;&semi;&NewLine; uniqueId &equals; &lpar;TextView&rpar; itemView&period;findViewById&lpar;R&period;id&period;tv&lowbar;id&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void bindTo&lpar;Country country&rpar; &lbrace;&NewLine; countryName&period;setText&lpar;country&period;countryName&rpar;&semi;&NewLine; uniqueId&period;setText&lpar;Integer&period;toString&lpar;country&period;id&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version