├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── sharmadhiraj │ │ └── androidpaginglibrarystepbystepimplementationguide │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── sharmadhiraj │ │ │ └── androidpaginglibrarystepbystepimplementationguide │ │ │ ├── activity │ │ │ └── NewsListActivity.kt │ │ │ ├── adapter │ │ │ ├── ListFooterViewHolder.kt │ │ │ ├── NewsListAdapter.kt │ │ │ └── NewsViewHolder.kt │ │ │ ├── data │ │ │ ├── NetworkService.kt │ │ │ ├── NewsDataSource.kt │ │ │ ├── NewsDataSourceFactory.kt │ │ │ ├── Response.kt │ │ │ └── State.kt │ │ │ └── viewModel │ │ │ └── NewsListViewModel.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_news_list.xml │ │ ├── item_list_footer.xml │ │ └── item_news.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── sharmadhiraj │ └── androidpaginglibrarystepbystepimplementationguide │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Paging Library Step By Step Implementation Guide 2 | Please follow this article on medium: 3 | [Android Paging Library Step By Step Implementation Guide](https://medium.com/@sharmadhiraj.np/android-paging-library-step-by-step-implementation-guide-75417753d9b9) 4 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 30 7 | defaultConfig { 8 | applicationId "com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide" 9 | minSdkVersion 16 10 | targetSdkVersion 30 11 | multiDexEnabled true 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | namespace 'com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide' 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | 28 | //Kotlin 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | 31 | //Support 32 | implementation 'androidx.appcompat:appcompat:1.3.0' 33 | implementation 'com.google.android.material:material:1.3.0' 34 | implementation 'androidx.recyclerview:recyclerview:1.2.0' 35 | implementation 'androidx.cardview:cardview:1.0.0' 36 | 37 | //Architecture Components 38 | implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' 39 | 40 | //Paging 41 | implementation 'androidx.paging:paging-runtime-ktx:3.0.0' 42 | 43 | //Networking 44 | implementation 'com.squareup.retrofit2:retrofit:2.9.0' 45 | implementation 'com.squareup.retrofit2:converter-gson:2.9.0' 46 | implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0' 47 | 48 | //Rx 49 | implementation 'io.reactivex.rxjava2:rxjava:2.2.10' 50 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' 51 | 52 | //Image 53 | implementation 'com.squareup.picasso:picasso:2.71828' 54 | 55 | //Testing 56 | testImplementation 'junit:junit:4.13.2' 57 | androidTestImplementation 'androidx.test:runner:1.3.0' 58 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 59 | } 60 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide 2 | 3 | import androidx.test.InstrumentationRegistry 4 | import androidx.test.runner.AndroidJUnit4 5 | import org.junit.Assert.assertEquals 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | /** 10 | * Instrumented test, which will execute on an Android device. 11 | * 12 | * See [testing documentation](http://d.android.com/tools/testing). 13 | */ 14 | @RunWith(AndroidJUnit4::class) 15 | class ExampleInstrumentedTest { 16 | @Test 17 | fun useAppContext() { 18 | // Context of the app under test. 19 | val appContext = InstrumentationRegistry.getTargetContext() 20 | assertEquals("com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide", appContext.packageName) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/activity/NewsListActivity.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.activity 2 | 3 | import android.os.Bundle 4 | import android.view.View.GONE 5 | import android.view.View.VISIBLE 6 | import androidx.appcompat.app.AppCompatActivity 7 | import androidx.lifecycle.Observer 8 | import androidx.lifecycle.ViewModelProvider 9 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.R 10 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.adapter.NewsListAdapter 11 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State 12 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.ERROR 13 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.LOADING 14 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.viewModel.NewsListViewModel 15 | import kotlinx.android.synthetic.main.activity_news_list.* 16 | 17 | class NewsListActivity : AppCompatActivity() { 18 | 19 | private lateinit var viewModel: NewsListViewModel 20 | private lateinit var newsListAdapter: NewsListAdapter 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_news_list) 25 | 26 | viewModel = ViewModelProvider(this).get(NewsListViewModel::class.java) 27 | initAdapter() 28 | initState() 29 | } 30 | 31 | private fun initAdapter() { 32 | newsListAdapter = NewsListAdapter { viewModel.retry() } 33 | recycler_view.adapter = newsListAdapter 34 | viewModel.newsList.observe(this, 35 | Observer { 36 | newsListAdapter.submitList(it) 37 | }) 38 | } 39 | 40 | private fun initState() { 41 | txt_error.setOnClickListener { viewModel.retry() } 42 | viewModel.getState().observe(this, Observer { state -> 43 | progress_bar.visibility = 44 | if (viewModel.listIsEmpty() && state == LOADING) VISIBLE else GONE 45 | txt_error.visibility = if (viewModel.listIsEmpty() && state == ERROR) VISIBLE else GONE 46 | if (!viewModel.listIsEmpty()) { 47 | newsListAdapter.setState(state ?: State.DONE) 48 | } 49 | }) 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/adapter/ListFooterViewHolder.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.adapter 2 | 3 | import android.view.LayoutInflater 4 | import android.view.View 5 | import android.view.View.INVISIBLE 6 | import android.view.View.VISIBLE 7 | import android.view.ViewGroup 8 | import androidx.recyclerview.widget.RecyclerView 9 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.R 10 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State 11 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.ERROR 12 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.LOADING 13 | import kotlinx.android.synthetic.main.item_list_footer.view.progress_bar 14 | import kotlinx.android.synthetic.main.item_list_footer.view.txt_error 15 | 16 | class ListFooterViewHolder(view: View) : RecyclerView.ViewHolder(view) { 17 | 18 | fun bind(status: State?) { 19 | itemView.progress_bar.visibility = if (status == LOADING) VISIBLE else INVISIBLE 20 | itemView.txt_error.visibility = if (status == ERROR) VISIBLE else INVISIBLE 21 | } 22 | 23 | companion object { 24 | fun create(retry: () -> Unit, parent: ViewGroup): ListFooterViewHolder { 25 | val view = LayoutInflater.from(parent.context) 26 | .inflate(R.layout.item_list_footer, parent, false) 27 | view.txt_error.setOnClickListener { retry() } 28 | return ListFooterViewHolder(view) 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/adapter/NewsListAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.adapter 2 | 3 | import android.view.ViewGroup 4 | import androidx.paging.PagedListAdapter 5 | import androidx.recyclerview.widget.DiffUtil 6 | import androidx.recyclerview.widget.RecyclerView 7 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.News 8 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State 9 | 10 | class NewsListAdapter(private val retry: () -> Unit) 11 | : PagedListAdapter(NewsDiffCallback) { 12 | 13 | private val DATA_VIEW_TYPE = 1 14 | private val FOOTER_VIEW_TYPE = 2 15 | 16 | private var state = State.LOADING 17 | 18 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { 19 | return if (viewType == DATA_VIEW_TYPE) NewsViewHolder.create(parent) else ListFooterViewHolder.create( 20 | retry, 21 | parent 22 | ) 23 | } 24 | 25 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { 26 | if (getItemViewType(position) == DATA_VIEW_TYPE) 27 | (holder as NewsViewHolder).bind(getItem(position)) 28 | else (holder as ListFooterViewHolder).bind(state) 29 | } 30 | 31 | override fun getItemViewType(position: Int): Int { 32 | return if (position < super.getItemCount()) DATA_VIEW_TYPE else FOOTER_VIEW_TYPE 33 | } 34 | 35 | companion object { 36 | val NewsDiffCallback = object : DiffUtil.ItemCallback() { 37 | override fun areItemsTheSame(oldItem: News, newItem: News): Boolean { 38 | return oldItem.title == newItem.title 39 | } 40 | 41 | override fun areContentsTheSame(oldItem: News, newItem: News): Boolean { 42 | return oldItem == newItem 43 | } 44 | } 45 | } 46 | 47 | override fun getItemCount(): Int { 48 | return super.getItemCount() + if (hasFooter()) 1 else 0 49 | } 50 | 51 | private fun hasFooter(): Boolean { 52 | return super.getItemCount() != 0 && (state == State.LOADING || state == State.ERROR) 53 | } 54 | 55 | fun setState(state: State) { 56 | this.state = state 57 | notifyItemChanged(super.getItemCount()) 58 | } 59 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/adapter/NewsViewHolder.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.adapter 2 | 3 | import android.view.LayoutInflater 4 | import android.view.View 5 | import android.view.ViewGroup 6 | import androidx.recyclerview.widget.RecyclerView 7 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.R 8 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.News 9 | import com.squareup.picasso.Picasso 10 | import kotlinx.android.synthetic.main.item_news.view.img_news_banner 11 | import kotlinx.android.synthetic.main.item_news.view.txt_news_name 12 | 13 | class NewsViewHolder(view: View) : RecyclerView.ViewHolder(view) { 14 | 15 | fun bind(news: News?) { 16 | if (news != null) { 17 | itemView.txt_news_name.text = news.title 18 | if (!news.image.isNullOrEmpty()) 19 | Picasso.get().load(news.image).into(itemView.img_news_banner) 20 | } 21 | } 22 | 23 | companion object { 24 | fun create(parent: ViewGroup): NewsViewHolder { 25 | val view = LayoutInflater.from(parent.context) 26 | .inflate(R.layout.item_news, parent, false) 27 | return NewsViewHolder(view) 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/data/NetworkService.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data 2 | 3 | import io.reactivex.Single 4 | import retrofit2.Retrofit 5 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 6 | import retrofit2.converter.gson.GsonConverterFactory 7 | import retrofit2.http.GET 8 | import retrofit2.http.Query 9 | 10 | interface NetworkService { 11 | 12 | @GET("everything?q=sports&apiKey=aa67d8d98c8e4ad1b4f16dbd5f3be348") 13 | fun getNews( 14 | @Query("page") page: Int, 15 | @Query("pageSize") pageSize: Int 16 | ): Single 17 | 18 | companion object { 19 | fun getService(): NetworkService { 20 | val retrofit = Retrofit.Builder() 21 | .baseUrl("https://newsapi.org/v2/") 22 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 23 | .addConverterFactory(GsonConverterFactory.create()) 24 | .build() 25 | return retrofit.create(NetworkService::class.java) 26 | } 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/data/NewsDataSource.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data 2 | 3 | import androidx.lifecycle.MutableLiveData 4 | import androidx.paging.PageKeyedDataSource 5 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.DONE 6 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.State.ERROR 7 | import io.reactivex.Completable 8 | import io.reactivex.android.schedulers.AndroidSchedulers 9 | import io.reactivex.disposables.CompositeDisposable 10 | import io.reactivex.functions.Action 11 | import io.reactivex.schedulers.Schedulers 12 | 13 | class NewsDataSource( 14 | private val networkService: NetworkService, 15 | private val compositeDisposable: CompositeDisposable 16 | ) : PageKeyedDataSource() { 17 | 18 | var state: MutableLiveData = MutableLiveData() 19 | private var retryCompletable: Completable? = null 20 | 21 | 22 | override fun loadInitial( 23 | params: LoadInitialParams, 24 | callback: LoadInitialCallback 25 | ) { 26 | updateState(State.LOADING) 27 | compositeDisposable.add( 28 | networkService.getNews(1, params.requestedLoadSize) 29 | .subscribe( 30 | { response -> 31 | updateState(DONE) 32 | callback.onResult( 33 | response.news, 34 | null, 35 | 2 36 | ) 37 | }, 38 | { 39 | updateState(ERROR) 40 | setRetry(Action { loadInitial(params, callback) }) 41 | } 42 | ) 43 | ) 44 | } 45 | 46 | override fun loadAfter(params: LoadParams, callback: LoadCallback) { 47 | updateState(State.LOADING) 48 | compositeDisposable.add( 49 | networkService.getNews(params.key, params.requestedLoadSize) 50 | .subscribe( 51 | { response -> 52 | updateState(DONE) 53 | callback.onResult( 54 | response.news, 55 | params.key + 1 56 | ) 57 | }, 58 | { 59 | updateState(ERROR) 60 | setRetry(Action { loadAfter(params, callback) }) 61 | } 62 | ) 63 | ) 64 | } 65 | 66 | override fun loadBefore(params: LoadParams, callback: LoadCallback) { 67 | } 68 | 69 | private fun updateState(state: State) { 70 | this.state.postValue(state) 71 | } 72 | 73 | fun retry() { 74 | if (retryCompletable != null) { 75 | compositeDisposable.add( 76 | retryCompletable!! 77 | .subscribeOn(Schedulers.io()) 78 | .observeOn(AndroidSchedulers.mainThread()) 79 | .subscribe() 80 | ) 81 | } 82 | } 83 | 84 | private fun setRetry(action: Action?) { 85 | retryCompletable = if (action == null) null else Completable.fromAction(action) 86 | } 87 | 88 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/data/NewsDataSourceFactory.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data 2 | 3 | import androidx.lifecycle.MutableLiveData 4 | import androidx.paging.DataSource 5 | import io.reactivex.disposables.CompositeDisposable 6 | 7 | class NewsDataSourceFactory( 8 | private val compositeDisposable: CompositeDisposable, 9 | private val networkService: NetworkService 10 | ) : DataSource.Factory() { 11 | 12 | val newsDataSourceLiveData = MutableLiveData() 13 | 14 | override fun create(): DataSource { 15 | val newsDataSource = NewsDataSource(networkService, compositeDisposable) 16 | newsDataSourceLiveData.postValue(newsDataSource) 17 | return newsDataSource 18 | } 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/data/Response.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | data class Response( 6 | @SerializedName("articles") 7 | val news: List 8 | ) 9 | 10 | data class News( 11 | val title: String, 12 | @SerializedName("urlToImage") 13 | val image: String? 14 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/data/State.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data 2 | 3 | enum class State { 4 | DONE, LOADING, ERROR 5 | } -------------------------------------------------------------------------------- /app/src/main/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/viewModel/NewsListViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.viewModel 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.Transformations 5 | import androidx.lifecycle.ViewModel 6 | import androidx.paging.LivePagedListBuilder 7 | import androidx.paging.PagedList 8 | import com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide.data.* 9 | import io.reactivex.disposables.CompositeDisposable 10 | 11 | class NewsListViewModel : ViewModel() { 12 | 13 | private val networkService = NetworkService.getService() 14 | var newsList: LiveData> 15 | private val compositeDisposable = CompositeDisposable() 16 | private val pageSize = 5 17 | private val newsDataSourceFactory: NewsDataSourceFactory = 18 | NewsDataSourceFactory(compositeDisposable, networkService) 19 | 20 | init { 21 | val config = PagedList.Config.Builder() 22 | .setPageSize(pageSize) 23 | .setInitialLoadSizeHint(pageSize * 2) 24 | .setEnablePlaceholders(false) 25 | .build() 26 | newsList = LivePagedListBuilder(newsDataSourceFactory, config).build() 27 | } 28 | 29 | 30 | fun getState(): LiveData = Transformations.switchMap( 31 | newsDataSourceFactory.newsDataSourceLiveData, 32 | NewsDataSource::state 33 | ) 34 | 35 | fun retry() { 36 | newsDataSourceFactory.newsDataSourceLiveData.value?.retry() 37 | } 38 | 39 | fun listIsEmpty(): Boolean { 40 | return newsList.value?.isEmpty() ?: true 41 | } 42 | 43 | override fun onCleared() { 44 | super.onCleared() 45 | compositeDisposable.dispose() 46 | } 47 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_news_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 20 | 21 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_list_footer.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_news.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 17 | 18 | 24 | 25 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android Paging Library Step By Step Implementation Guide 3 | Error !! Tap to retry. 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/sharmadhiraj/androidpaginglibrarystepbystepimplementationguide/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.sharmadhiraj.androidpaginglibrarystepbystepimplementationguide 2 | 3 | import org.junit.Assert.assertEquals 4 | import org.junit.Test 5 | 6 | /** 7 | * Example local unit test, which will execute on the development machine (host). 8 | * 9 | * See [testing documentation](http://d.android.com/tools/testing). 10 | */ 11 | class ExampleUnitTest { 12 | @Test 13 | fun addition_isCorrect() { 14 | assertEquals(4, 2 + 2) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.7.10' 3 | repositories { 4 | google() 5 | mavenCentral() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:7.4.2' 9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 10 | } 11 | } 12 | 13 | allprojects { 14 | repositories { 15 | google() 16 | mavenCentral() 17 | } 18 | } 19 | 20 | task clean(type: Delete) { 21 | delete rootProject.buildDir 22 | } 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | android.enableJetifier=true 10 | android.useAndroidX=true 11 | org.gradle.jvmargs=-Xmx1536m 12 | # When configured, Gradle will run in incubating parallel mode. 13 | # This option should only be used with decoupled projects. More details, visit 14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 15 | # org.gradle.parallel=true 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide/85b1517791740b006d636e7ab33662e360b0dd31/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 84 | 85 | APP_NAME="Gradle" 86 | APP_BASE_NAME=${0##*/} 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | MAX_FD=$( ulimit -H -n ) || 147 | warn "Could not query maximum file descriptor limit" 148 | esac 149 | case $MAX_FD in #( 150 | '' | soft) :;; #( 151 | *) 152 | ulimit -n "$MAX_FD" || 153 | warn "Could not set maximum file descriptor limit to $MAX_FD" 154 | esac 155 | fi 156 | 157 | # Collect all arguments for the java command, stacking in reverse order: 158 | # * args from the command line 159 | # * the main class name 160 | # * -classpath 161 | # * -D...appname settings 162 | # * --module-path (only if needed) 163 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 164 | 165 | # For Cygwin or MSYS, switch paths to Windows format before running java 166 | if "$cygwin" || "$msys" ; then 167 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 168 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 169 | 170 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 171 | 172 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 173 | for arg do 174 | if 175 | case $arg in #( 176 | -*) false ;; # don't mess with options #( 177 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 178 | [ -e "$t" ] ;; #( 179 | *) false ;; 180 | esac 181 | then 182 | arg=$( cygpath --path --ignore --mixed "$arg" ) 183 | fi 184 | # Roll the args list around exactly as many times as the number of 185 | # args, so each arg winds up back in the position where it started, but 186 | # possibly modified. 187 | # 188 | # NB: a `for` loop captures its iteration list before it begins, so 189 | # changing the positional parameters here affects neither the number of 190 | # iterations, nor the values presented in `arg`. 191 | shift # remove old arg 192 | set -- "$@" "$arg" # push replacement arg 193 | done 194 | fi 195 | 196 | # Collect all arguments for the java command; 197 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 198 | # shell script including quotes and variable substitutions, so put them in 199 | # double quotes to make sure that they get re-expanded; and 200 | # * put everything else in single quotes, so that it's not re-expanded. 201 | 202 | set -- \ 203 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 204 | -classpath "$CLASSPATH" \ 205 | org.gradle.wrapper.GradleWrapperMain \ 206 | "$@" 207 | 208 | # Use "xargs" to parse quoted args. 209 | # 210 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 211 | # 212 | # In Bash we could simply go: 213 | # 214 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 215 | # set -- "${ARGS[@]}" "$@" 216 | # 217 | # but POSIX shell has neither arrays nor command substitution, so instead we 218 | # post-process each arg (as a line of input to sed) to backslash-escape any 219 | # character that might be a shell metacharacter, then use eval to reverse 220 | # that process (while maintaining the separation between arguments), and wrap 221 | # the whole thing up as a single "set" statement. 222 | # 223 | # This will of course break if any of these variables contains a newline or 224 | # an unmatched quote. 225 | # 226 | 227 | eval "set -- $( 228 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 229 | xargs -n1 | 230 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 231 | tr '\n' ' ' 232 | )" '"$@"' 233 | 234 | exec "$JAVACMD" "$@" 235 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------