Created
March 10, 2019 06:21
-
-
Save yigit/0c90c7a9ee8c4c23136586c537621a25 to your computer and use it in GitHub Desktop.
recyclerview that does not remove on swipe
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 com.example.android.recyclerviewswipe | |
import android.content.Context | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.appcompat.widget.AppCompatCheckBox | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ItemTouchHelper | |
import androidx.recyclerview.widget.ListAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val adapter = ItemAdapter() | |
adapter.submitList(ITEMS) | |
list.adapter = adapter | |
val touchHelper = ItemTouchHelper( | |
object : ItemTouchHelper.Callback() { | |
override fun getMovementFlags( | |
recyclerView: RecyclerView, | |
viewHolder: RecyclerView.ViewHolder | |
): Int { | |
return makeMovementFlags( | |
0, | |
ItemTouchHelper.END | |
) | |
} | |
override fun onMove( | |
recyclerView: RecyclerView, | |
viewHolder: RecyclerView.ViewHolder, | |
target: RecyclerView.ViewHolder | |
): Boolean { | |
return false | |
} | |
override fun clearView( | |
recyclerView: RecyclerView, | |
viewHolder: RecyclerView.ViewHolder | |
) { | |
super.clearView(recyclerView, viewHolder) | |
} | |
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | |
if (viewHolder is ItemViewHolder) { | |
viewHolder.item?.let { | |
val id = it.id | |
adapter.submitList( | |
adapter.currentList.map { | |
if (it.id == id) { | |
it.copy( | |
checked = !it.checked | |
) | |
} else { | |
it | |
} | |
} | |
) | |
} | |
} | |
} | |
} | |
) | |
touchHelper.attachToRecyclerView(list) | |
} | |
} | |
data class Item( | |
val id: Int, | |
val value: String, | |
val checked: Boolean | |
) | |
class ItemViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder( | |
LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false) | |
) { | |
val checkbox = itemView as AppCompatCheckBox | |
var item: Item? = null | |
} | |
class ItemAdapter : ListAdapter<Item, ItemViewHolder>( | |
object : DiffUtil.ItemCallback<Item>() { | |
override fun areItemsTheSame(oldItem: Item, newItem: Item): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: Item, newItem: Item): Boolean { | |
return oldItem == newItem | |
} | |
} | |
) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { | |
return ItemViewHolder(parent) | |
} | |
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | |
val item = getItem(position) | |
holder.checkbox.isChecked = item.checked | |
holder.checkbox.text = item.value | |
holder.item = item | |
} | |
} | |
val CHEESE_NAMES = listOf( | |
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", | |
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", | |
"Aisy Cendre", "Allgauer Emmentaler", "Alverca" | |
) | |
val ITEMS = CHEESE_NAMES.mapIndexed { index, name -> | |
Item(index, name, false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment