This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation "androidx.navigation:navigation-fragment-ktx:2.2.2" | |
implementation "androidx.navigation:navigation-ui-ktx:2.2.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyAdapter : PagingDataAdapter<MyModel, MyAdapter.MyViewHolder>(DiffUtilCallback()) { | |
// the adapter implementation still the same | |
} | |
class DiffUtilCallBack : DiffUtil.ItemCallback<MyModel>() { | |
override fun areItemsTheSame(oldItem: MyModel, newItem: MyModel): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: MyModel, newItem: MyModel): Boolean { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.xpto.custom.ui.view; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.support.annotation.DrawableRes; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyAdapter : PagingDataAdapter<MyModel, MyAdapter.MyViewHolder>(DiffUtilCallback()) { | |
// a implementação do corpo do adapter permanece a mesma | |
} | |
class DiffUtilCallBack : DiffUtil.ItemCallback<MyModel>() { | |
override fun areItemsTheSame(oldItem: MyModel, newItem: MyModel): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: MyModel, newItem: MyModel): Boolean { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getData() { | |
lifecycleScope.launch { | |
viewModel.getItems().collectLatest { | |
adapter.submitData(it) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getItems(): Flow<PagingData<MyModel>> { | |
return Pager( | |
config = PagingConfig(pageSize = 20), | |
pagingSourceFactory = { MyPagingSource(api = api) } | |
).flow.cachedIn(viewModelScope) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyPagingSource( | |
private val api: MyApi | |
) : PagingSource<Int, MyModel>() { | |
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MyModel> { | |
return try { | |
val result = api.getItems(params.key ?: STARTING_PAGE_INDEX) | |
Page( | |
data = result, | |
prevKey = params.key, | |
nextKey = result.nextPage ?: STARTING_PAGE_INDEX.plus(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.annotation.SuppressLint | |
import android.content.Context | |
import android.content.res.ColorStateList | |
import android.os.Build | |
import android.support.annotation.RequiresApi | |
import android.support.design.widget.TextInputLayout | |
import android.support.v7.widget.AppCompatEditText |