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

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message