Giter Site home page Giter Site logo

Comments (1)

devAseemSharma avatar devAseemSharma commented on May 18, 2024

There are multiple ways, it can be done in one of the callbacks, you will need to add the unique identifier of the selected item (needs to be the field of your Model) in the preference and then later extract it, given this item's list is already stored somewhere.

Another way is to use some kind of Json parser framework like Gson or Moshi to convert the model to jsontString save that string and then when you read that string from preference convert it back to your model.

For this example I am putting all the selected items in listItemSelected ArrayList you can use this list to store it in preference.

In this example there is a interface callback which is provided to rvSelectedSetting touchHelper look for the following code:

 override fun onDrag(v: View?, event: DragEvent): Boolean {
        val state = event.localState as DragData
        when (event.action) {
            DragEvent.ACTION_DRAG_ENTERED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.green_dashed_box)
            }
            DragEvent.ACTION_DRAG_EXITED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.dashed_box)
            }
            DragEvent.ACTION_DRAG_ENDED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.dashed_box)
            }
            DragEvent.ACTION_DROP -> {
                if (listItemSelected.find { it.id == state.item.id } != null) {
                    Toast.makeText(MainActivity@ this, "Setting already exists!", Toast.LENGTH_LONG)
                        .show()
                    return true
                }
  
                listItemSelected.add(0, state.item) // All the selected items getting stored in this list. 
                selectedSettingAdapter.notifyItemRangeInserted(0, 1)
            }
            else -> {}
        }
        return true
    }

from drag-and-drop-sample.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.