项目作者: utsmannn

项目描述 :
Android Paging Library with painless implementation
高级语言: Kotlin
项目地址: git://github.com/utsmannn/painless-paging-library.git
创建时间: 2021-04-02T20:40:44Z
项目社区:https://github.com/utsmannn/painless-paging-library

开源协议:Apache License 2.0

下载



Painless Paging Library





bintray
License
Pull request
Twitter
Github

Android Paging Library with painless implementation
Build for modern architecture with Kotlin and Coroutine


Download

Step 1 - Add the JitPack repository to your build file

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Step 2 - Add the dependency

  1. dependencies {
  2. implementation('com.github.utsmannn:painless-paging-library:{latest_version}') {
  3. exclude group: 'com.google.android.material', module: 'material'
  4. }
  5. }

Implementation

Create Adapter

The adapter extend to PainlessPagedAdapter

  1. // standard view holder
  2. class UserViewHolder(view: View) : RecyclerView.ViewHolder(view) {
  3. fun bind(item: User, position: Int) = itemView.run {
  4. findViewById<TextView>(R.id.txt_item).text = "$position - ${item.name}"
  5. }
  6. }
  7. // adapter with PainlessPagedAdapter
  8. class UserAdapter : PainlessPagedAdapter<User, UserViewHolder>() {
  9. override fun onCreatePageViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
  10. return UserViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false))
  11. }
  12. override fun onBindPageViewHolder(holder: UserViewHolder, position: Int) {
  13. getItem(position)?.let {
  14. holder.bind(it, position)
  15. }
  16. }
  17. }

Create data source

Create class with extend to PagingDataSource

  1. class UserDataSource : PagingDataSource<User>() {
  2. private val userRepository: UserRepository = UserRepository.Companion.Impl()
  3. override suspend fun onLoadState(page: Int): PagingResult {
  4. return try {
  5. val response = userRepository.getUsers(page)
  6. val items = response.data
  7. PagingResult.Success(items ?: emptyList())
  8. } catch (e: Throwable) {
  9. PagingResult.Error(e)
  10. }
  11. }
  12. }

Add in ViewModel

The data source will be extracting result item with PagingData class and wrapping with liveData, implement it on ViewModel.

  1. class UserViewModel : ViewModel() {
  2. private val userDataSource = UserDataSource()
  3. val pageData: LiveData<PagingData<User>>
  4. get() = userDataSource.currentPageLiveData()
  5. }

Submitting item on adapter

  1. viewModel.pageData.observe(this) { pagingData ->
  2. userAdapter.submitData(pagingData)
  3. }

Extensions

This library can generate adapter with simple extensions code. Not recommended for multiple view type adapter.

  1. val userAdapter = recyclerView.createSimpleAdapter<User>(R.layout.item_view) {
  2. layoutManager = LinearLayoutManager(this@MainActivity)
  3. onBindViewHolder = { itemView, item, position ->
  4. itemView.run {
  5. findViewById<TextView>(R.id.txt_item).text = "$position - ${item.name}"
  6. }
  7. }
  8. }

Add State changes UI

You can add the loading view in footer, state of data changes.

Create a standard view holder

Place all view when data change to loading and error

  1. class StateViewHolder(view: View) : RecyclerView.ViewHolder(view) {
  2. fun bind(loadState: LoadState, retry: () -> Unit) = itemView.run {
  3. findViewById<ProgressBar>(R.id.progress_bar).run {
  4. isVisible = loadState.loadStatus == LoadStatus.RUNNING
  5. }
  6. findViewById<TextView>(R.id.txt_error).run {
  7. isVisible = loadState.loadStatus == LoadStatus.FAILED
  8. if (loadState.loadStatus == LoadStatus.FAILED) {
  9. text = (loadState as LoadState.Failed).throwable?.message
  10. }
  11. }
  12. findViewById<Button>(R.id.btn_retry).run {
  13. isVisible = loadState.loadStatus == LoadStatus.FAILED
  14. setOnClickListener {
  15. retry.invoke()
  16. }
  17. }
  18. }
  19. }

Attach state view holder in adapter

  1. userAdapter.attachStateViewHolder { parent ->
  2. val view = LayoutInflater.from(parent.context).inflate(R.layout.state_view, parent, false)
  3. StateViewHolder(view)
  4. }
  5. userAdapter.onBindLoadStateViewHolder<StateViewHolder> { holder, loadState ->
  6. holder.bind(loadState) {
  7. userAdapter.retry()
  8. }
  9. }

License

  1. Copyright 2021 Muhammad Utsman
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.