-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
PagingActivity.kt
64 lines (52 loc) · 2.03 KB
/
PagingActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package net.gotev.recycleradapterdemo.activities
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import net.gotev.recycleradapter.paging.PagingAdapter
import net.gotev.recycleradapterdemo.App
import net.gotev.recycleradapterdemo.R
import net.gotev.recycleradapterdemo.network.api.StarWarsPeopleDataSource
class PagingActivity : AppCompatActivity() {
companion object {
fun show(activity: AppCompatActivity) {
activity.startActivity(Intent(activity, PagingActivity::class.java))
}
}
private lateinit var pagingAdapter: PagingAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_paging)
title = getString(R.string.paged_scrolling)
supportActionBar?.apply {
setHomeButtonEnabled(true)
setDisplayHomeAsUpEnabled(true)
}
pagingAdapter = PagingAdapter(
dataSource = { StarWarsPeopleDataSource(App.starWarsClient) },
config = PagedList.Config.Builder()
.setPageSize(20)
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setMaxSize(50)
.build()
)
findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(this@PagingActivity, RecyclerView.VERTICAL, false)
adapter = pagingAdapter
}
findViewById<SwipeRefreshLayout>(R.id.swipeRefresh).apply {
setOnRefreshListener {
pagingAdapter.reload()
}
pagingAdapter.startObserving(
this@PagingActivity,
onLoadingComplete = { isRefreshing = false }
)
isRefreshing = true
}
}
}