Created
August 12, 2020 15:50
-
-
Save informramiz/9e6aec21029c33d173dbfbc9ec89d642 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MainActivity : AppCompatActivity() { | |
| private val items = mutableListOf("Item1", "Item2", "Item3", "Item4", "Item5", "Item6") | |
| private val simpleAdapter = SimpleAdapter() | |
| private val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT) { | |
| override fun onMove( | |
| recyclerView: RecyclerView, | |
| viewHolder: RecyclerView.ViewHolder, | |
| target: RecyclerView.ViewHolder | |
| ): Boolean { | |
| val fromPosition = viewHolder.adapterPosition | |
| val toPosition = target.adapterPosition | |
| recyclerView.adapter?.notifyItemMoved(fromPosition, toPosition) | |
| return true | |
| } | |
| override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | |
| items.removeAt(viewHolder.adapterPosition) | |
| list.adapter?.notifyItemRemoved(viewHolder.adapterPosition) | |
| } | |
| }) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| list.adapter = simpleAdapter | |
| itemTouchHelper.attachToRecyclerView(list) | |
| list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) | |
| simpleAdapter.submitList(items) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment