August 13, 2021

Multi-feature Recyclerview in android – Swipe to show options, drag (with handle also), filter enabled

By lj007

Whats new added in this:

1) Swipe item to show option like delete, show, edit

2) Custom layout for options by xml only, no code for view

3) Drag item to re-position

4) Rotate while dragging

5) Drag item with view like handle to drag

6) Search filter optimized

Here is example usage of BaseRecyclerView. Check code on github if want to check actual code of BaseRecyclerView.

import `in`.thejadav.baserecyclerview.BaseViewHolder
import android.graphics.Color
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.item_example.view.*

class ExampleListHolder(view: View) : BaseViewHolder(view) {

    init {
        itemView.ivDelete.setOnClickListener {
            listener?.holderItemClicked(adapterPosition, it.id, Bundle())
        }
    }

    override fun setItem(t: String) {
        itemView.textView.text = t
    }

    override fun itemSelected() {
        super.itemSelected()
        itemView.setBackgroundColor(Color.LTGRAY)
    }

    override fun itemCleared() {
        super.itemCleared()
        itemView.setBackgroundColor(Color.WHITE)
    }
}

import `in`.thejadav.baserecyclerview.BaseRvAdapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup

class ExmpleStringAdapter(handleId: Int, hiddenViewId: Int, mainViewId: Int, var listener: ExampleStringListener) : BaseRvAdapter(handleId = handleId, hiddenViewId = hiddenViewId, mainViewId = mainViewId, dragEnabled = true) {
    override fun holderItemClicked(position: Int, viewId: Int?, bundle: Bundle?) {
        listener.deleteItem(list[position])
    }

    override fun itemMoved(position: Int) {
        listener.itemDraged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleListHolder {
        return ExampleListHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_example, parent, false))
    }
}

interface ExampleStringListener{
    fun deleteItem(s: String)
    fun itemDraged()
}

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), ExampleStringListener {
    private lateinit var adapter: ExmpleStringAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        adapter = ExmpleStringAdapter(R.id.ivHandle, R.id.child, R.id.parent,this)
        rvExample.layoutManager = LinearLayoutManager(this)
        rvExample.adapter = adapter

        val myList = arrayListOf("Hello", "Good Morning", "How", "Are", "You","Hello", "Good Morning", "How", "Are", "You")
        adapter.replaceAll(myList)
    }

    override fun deleteItem(s: String) {
        Toast.makeText(this, "Delete $s", Toast.LENGTH_SHORT).show()
    }

    override fun itemDraged() {
        Toast.makeText(this, "Item Draged", Toast.LENGTH_SHORT).show()
    }
}