Glimpse: Use stable IDs in our adapters
Our album IDs and photo IDs are unique and stable. Change-Id: Ic37051ea71b3f773fc3dc241d1b513d636b8688c
This commit is contained in:
parent
238e5754a1
commit
8720344ae4
|
@ -23,6 +23,10 @@ class AlbumThumbnailAdapter(
|
||||||
) : RecyclerView.Adapter<AlbumThumbnailAdapter.AlbumViewHolder>() {
|
) : RecyclerView.Adapter<AlbumThumbnailAdapter.AlbumViewHolder>() {
|
||||||
private var albums: Array<Album>? = null
|
private var albums: Array<Album>? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
setHasStableIds(true)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
val layoutInflater = LayoutInflater.from(parent.context)
|
||||||
|
|
||||||
|
@ -33,6 +37,8 @@ class AlbumThumbnailAdapter(
|
||||||
|
|
||||||
override fun getItemCount() = albums?.size ?: 0
|
override fun getItemCount() = albums?.size ?: 0
|
||||||
|
|
||||||
|
override fun getItemId(position: Int) = (albums?.let { it[position].id } ?: 0).toLong()
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||||
albums?.let {
|
albums?.let {
|
||||||
holder.bind(it[position])
|
holder.bind(it[position])
|
||||||
|
|
|
@ -27,8 +27,14 @@ class ThumbnailAdapter(
|
||||||
|
|
||||||
private var recyclerView: RecyclerView? = null
|
private var recyclerView: RecyclerView? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
setHasStableIds(true)
|
||||||
|
}
|
||||||
|
|
||||||
override fun getItemCount() = super.getItemCount() + headersPositions.size
|
override fun getItemCount() = super.getItemCount() + headersPositions.size
|
||||||
|
|
||||||
|
override fun getItemId(position: Int) = getIdFromMediaStore(position)
|
||||||
|
|
||||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||||
super.onAttachedToRecyclerView(recyclerView)
|
super.onAttachedToRecyclerView(recyclerView)
|
||||||
|
|
||||||
|
@ -121,6 +127,13 @@ class ThumbnailAdapter(
|
||||||
recyclerView?.post { notifyItemInserted(newItemCount) }
|
recyclerView?.post { notifyItemInserted(newItemCount) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getIdFromMediaStore(position: Int): Long {
|
||||||
|
val cursor = cursor ?: return 0
|
||||||
|
val idIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID)
|
||||||
|
cursor.moveToPosition(getTruePosition(position))
|
||||||
|
return cursor.getLong(idIndex)
|
||||||
|
}
|
||||||
|
|
||||||
private fun getMediaFromMediaStore(position: Int): Media? {
|
private fun getMediaFromMediaStore(position: Int): Media? {
|
||||||
val cursor = cursor ?: return null
|
val cursor = cursor ?: return null
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue