recyclerview Drag and drop to different View

Viewed 105

I have a recyclerview in my project, where a user will be able to drag and drop it to a different view(card view). I am following mvvm pattern and using dataBinding. I use the longclick function from within the activity,i created an interface in adapter class for this.

inorder to use the drag and drop i need to get the adapter position of the item in activity, which i am unable to get.

here is the adapter class

class StatementAdapter(
    private val context: Context,
    private val statementList: ArrayList<Statement>
) :
    RecyclerView.Adapter<StatementAdapter.StatementViewHolder>() {

    private var mListener: OnItemLongClickListener? = null

    interface OnItemLongClickListener {
        fun onItemLongClick(statement: Statement)


    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener) {
        mListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatementViewHolder {

        val layoutInflater = LayoutInflater.from(parent.context)
        val statementBinding: StatementBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.statement_list, parent, false)

        return StatementViewHolder(statementBinding, mListener)

    }


    override fun getItemCount(): Int {
        return statementList.size
    }

    override fun onBindViewHolder(holder: StatementViewHolder, position: Int) {
        val statementViewModel = statementList[position]
        holder.bind(statementViewModel)
    }

    class StatementViewHolder(
        private val statementBinding: StatementBinding,
        private val listener: OnItemLongClickListener?

    ) : RecyclerView.ViewHolder(statementBinding.root) {

        fun bind(statementViewModel: Statement) {
            this.statementBinding.statementModel = statementViewModel
            itemView.setOnLongClickListener {
                listener?.onItemLongClick(
                    statementViewModel
                )
                true
            }
            statementBinding.executePendingBindings()
        }

    }

}

Here is the activity

class TrueOrFalseActivity : AppCompatActivity(), StatementAdapter.OnItemLongClickListener {
    private lateinit var trueOrFalseBinding: ActivityTrueOrFalseBinding
    private var rvStatement: RecyclerView? = null
    private var statementAdapter: StatementAdapter? = null
    private val dragMessage = "Added"
    private val inBucket = "in bucket"
    private val offBucket = "not in bucket"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        trueOrFalseBinding = ActivityTrueOrFalseBinding.inflate(layoutInflater)
        setContentView(trueOrFalseBinding.root)

        rvStatement = findViewById(R.id.rvStatement)
        val statementViewModel = ViewModelProvider(this).get(StatementViewModel::class.java)
        statementViewModel.generateStatement()

        statementViewModel.newMStatementList.observe(this) {
            statementAdapter = StatementAdapter(this@TrueOrFalseActivity, it)
            rvStatement!!.layoutManager = LinearLayoutManager(this@TrueOrFalseActivity)
            rvStatement!!.adapter = statementAdapter
            statementAdapter?.setOnItemLongClickListener(this)

        }

    }

    override fun onItemLongClick(statement: Statement) {
        val item = ClipData.Item(dragMessage)
        val dragData = ClipData(
            dragMessage,
            arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN),
            item
        )
        val myShadow = MyDragShadowBuilder(this)
        
        // Here where i need to get the item position


    }
}

class MyDragShadowBuilder(trueOrFalseActivity: TrueOrFalseActivity) {

}

I am following the following documentation

https://developer.android.com/guide/topics/ui/drag-drop.html#AboutDragging

in this, the "view" is the parameter, but in my case it is arecyclerview item.

how do i solve this?

0 Answers
Related