Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- elementAt
- list
- animation
- Interface
- docker-compose
- 생명주기
- react native
- mongoose
- function
- union
- ConstraintLayout
- ReactNative
- Service
- vuex
- MINUS
- Foreign Key
- collection
- docker
- Kotlin
- enum
- class component
- Generic
- map
- lifecycle
- Swift
- AWS
- LiveData
- CLASS
- recyclerview
- Filter
Archives
- Today
- Total
개발 일기
paging library ? (페이징 라이브러리) 본문
Paging 라이브러리를 사용하면 앱의 RecyclerView 내 데이터를 점진적이고 매끄럽게 로드할 수 있습니다.
주요 구성요소 :)
- PagedList
- Paging 라이브러리에 핵심 구성 요소는 청크 또는 페이지를 비동기적으로 로드하는 PagedList입니다.
- DataSource class가 PagedList가 필요로 하는 데이터를 제공하고, list를 업데이트할 수 있도록 도와줍니다.
- DataSource
- 데이터의 snapshots을 PagedList에 load 하기 위한 기본 클래스입니다..
- subclass로 PositionalDataSource, ItemKeyedDataSource, PagedKeyedDataSource가 있습니다.
- DataSourceType
- PositionalDataSource ( 페이지 번호나 offset을 통하여 사용이 가능합니다.)
- ItemKeyedDataSource ( 페이지 아이템의 키값으로 기반으로 사용이 가능합니다. ex) 처음 마지막 아이템)
- PagedKeyedDataSource ( 페이지의 키값을 기반으로 Next / Previous) 필드를 가지는 경우 사용이 가능합니다.
- PagedListAdapter
- PagedListAdapter는 PagedList를 이용한 RecyclerView.Adapter 입니다.
- DiffUtil을 통해 비교 된 후, 변경된 값이 변한 아이템만 변경합니다.
- 비교연산은 별도에 Background 에서 이루어지고 결과를 main thread에서 ui 작업을 진행하게 됩니다.
종속성 선언 :)
다음과 같이 필요한 종속성을 build.gradle에 추가합니다.
dependencies {
def paging_version = "2.1.1"
implementation "androidx.paging:paging-runtime:$paging_version" // For Kotlin use paging-runtime-ktx
// alternatively - without Android dependencies for testing
testImplementation "androidx.paging:paging-common:$paging_version" // For Kotlin use paging-common-ktx
// optional - RxJava support
implementation "androidx.paging:paging-rxjava2:$paging_version" // For Kotlin use paging-rxjava2-ktx
}
간단한 사용 예시 :)
val pagedLists : LiveData<PagedList<MonthModel>>
val factory : CalendarDataSourceFactory by lazy {
CalendarDataSourceFactory(usecase , mcoroutine)
}
init {
val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(CodeUtils.Network.Code.CALENDAR_COUNT) // 초기 사이즈
.setPageSize(CodeUtils.Network.Code.CALENDAR_COUNT) // 페이지 아이템 갯수
.setPrefetchDistance(43) // 얼마나 남았을때 다음데이터 노출
.build()
// rx을 사용 할경우 RxPagedListBuilder 사용
pagedLists = LivePagedListBuilder(factory , config).build()
}
class CalendarDataSourceFactory(private val model : CalendarUseCaseImpl, private val scope : CoroutineScope) : androidx.paging.DataSource.Factory<String, MonthModel> (){
val mutableLiveData : MutableLiveData<CalendarDataSource> = MutableLiveData<CalendarDataSource>()
val dataSource : CalendarDataSource by lazy {
CalendarDataSource(model , scope)
}
override fun create(): androidx.paging.DataSource<String, MonthModel> {
mutableLiveData.postValue(dataSource)
return dataSource
}
}
class CalendarDataSource(private val model : CalendarUseCaseImpl, private val scope : CoroutineScope) : ItemKeyedDataSource<String , MonthModel>(){
override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<MonthModel>) {
// 처음 데이터 관련 호출
callback.onResult(list)
}
override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<MonthModel>) {
// 다음 데이터 관련 호출 관련 Key는 params.key 에서 얻을수 있습니다.
callback.onResult(list)
}
override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<MonthModel>) {
// 다음 데이터 관련 호출 관련 Key는 params.key 에서 얻을수 있습니다.
callback.onResult(list)
}
override fun getKey(item: MonthModel): String {
// 무슨 key를 할건지 여부!
return item.month
}
}
// 아래와 같은 방법으로 데이터 초기화가 가능합니다.
factory.dataSource.value?.invalidate()'Client > 안드로이드' 카테고리의 다른 글
| Broadcast Receiver ? (0) | 2020.03.26 |
|---|---|
| Service (0) | 2020.03.26 |
| Databinding ? (0) | 2020.03.21 |
| Android Manifest ? (0) | 2020.03.21 |
| WorkManager ? (0) | 2020.03.18 |
Comments