Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement, drag and drop and swipe to delete in recyclerview?

Tags:

android

kotlin

Hi I'm new to Kotlin I have almost 1 month programming in it, I have one recycler I'm trying to re order the cardviews in the recycler and delete the ones that I don't need by swipping it, how can I achieve this? could it be done with a library? I couldn't find one

this is what I have:

Adapter

    open class EmployeesAdapter(var context: Context, var employee:ArrayList<EmployeesTest>):BaseAdapter() {

    private class ViewHolder(row: View?) {

        var txt_user: TextView
        var user_job: TextView
        var img_user: ImageView
        var checBox: CheckBox

        init {
            this.txt_user = row?.findViewById(R.id.txt_user) as TextView
            this.img_user = row.findViewById(R.id.img_user) as ImageView
            this.user_job = row.findViewById(R.id.user_job) as TextView
            this.checBox = row.findViewById(R.id.checkBox) as CheckBox


        }
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        var view: View
        var viewHolder: ViewHolder
        if (convertView == null) {
            var layout = LayoutInflater.from(context)
            view = layout.inflate(R.layout.employee_list_item, parent, false)
            viewHolder = ViewHolder(view)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = view.tag as ViewHolder
        }
        var employees:EmployeesTest=getItem(position) as EmployeesTest
        viewHolder.txt_user.text = employees.nombre
        viewHolder.img_user.setImageResource(employees.profilePic)
        viewHolder.user_job.text = employees.cargo
        viewHolder.checBox.isChecked = false

        return view

    }

    override fun getItem(position: Int): Any {
        return employee[position]
    }

    override fun getItemId(position: Int): Long {
        return  position.toLong()
    }

    override fun getCount(): Int {
        return employee.count()
    }
}

Activity

    class EditEmployeeFragment : Fragment() {
    var tv_name: TextView? = null
    var rel_main: RelativeLayout? = null


    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view:View?=inflater!!.inflate(R.layout.edit_employee_fragment,container,false)

        var arrReports: ArrayList<EmployeesTest>? = ArrayList()
        arrReports?.add(EmployeesTest("Heber","Desarrollador",R.drawable.img_user))
        arrReports?.add(EmployeesTest("Mata","Desarrollador",R.drawable.img_user))
        arrReports?.add(EmployeesTest("Ernesto","Desarrollador",R.drawable.img_user))
        arrReports?.add(EmployeesTest("Toño","Desarrollador",R.drawable.img_user))

        var recyclerView:RecyclerView=view?.findViewById(R.id.recycler_edit_employees) as RecyclerView
        recyclerView.layoutManager = LinearLayoutManager(this.context, LinearLayout.VERTICAL, false)
        recyclerView.adapter = EditEmployeesAdapter( arrReports!!)
        return view
    }

}

thank you for your kind help

like image 955
Heber Solis Avatar asked Dec 03 '22 11:12

Heber Solis


1 Answers

val itemTouchHelperCallback =
            object :
                ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {

                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    noteViewModel.delete(noteAdapter.getNoteAt(viewHolder.adapterPosition))
                    Toast.makeText(
                        this@MainActivity,
                        getString(R.string.note_deleted),
                        Toast.LENGTH_SHORT
                    ).show()
                }

            }

        val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
        itemTouchHelper.attachToRecyclerView(rvNote)
like image 196
Sana Ebadi Avatar answered Dec 11 '22 17:12

Sana Ebadi