TheJadav

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

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

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()
    }
}

Share this content:

Share:

More Posts

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message