首页 > 解决方案 > How to change item position of a recycle view from within its adapter?

问题描述

Is it possible to change the item from within the adapter itself?

What am I trying to a achieve?

I have created a simple recycler view and it has a simple list item, but when the user clicks on the item a bottom sheet dialog gets displayed (that bottom sheet is being created in the adapter itself so it's different for every item) that bottom sheet dialog have a back and next button which can allow the user to move back and forth through the item view

When the user clicks on item -> bottom sheet dialog gets displayed -> if he presses next I have to show next following item with its bottom sheet opened.

How can I move from I item to another within the adapter?

标签: androidkotlinandroid-recyclerviewandroid-adapter

解决方案


you can pass position and list item as ArrayList to bottom sheet and update UI on next and previous buttons.

for example, you can create a method in your bottom sheet to update bottom sheet UI:

    var listItem = ArrayList<Objects>()
    var position = 0
    
    private fun updateUi() {
        val item = listItem[position]
        ///update ui with item
    }

on next button click:

  position += 1
    if (position == listItem.size)
    {
        // end of list
    } else
    {
        updateUi()
    }

and on pervious button click:

    position -= 1
    if (position == -1)
    {
        // end of list
    } else
    {
        updateUi()
    }

推荐阅读