Skip to content

SwappableRecyclerAdapter

Shanti Ranjan Das edited this page Apr 5, 2020 · 1 revision

Here is a working sample of lazy loading recycler view.

AdapterClass for recyclerview

class AdapterRecycler(recyclerView: RecyclerView):
    SwappableRecyclerAdapter<CustomModel, CustomViewHolder>(recyclerView) {

    override fun onBindHolder(holder: CustomViewHolder, data: CustomModel, position: Int) {
        holder.bindViews(data)
    }

    override fun onCreateHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        return CustomViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.layout_sample,
                parent,
                false
            )
        )
    }

    override fun onDragFromPosition(
        fromData: CustomModel,
        fromPosition: Int,
        toData: CustomModel,
        toPosition: Int
    ): Boolean {
        return true
    }

    override fun onSwipeRightOrLeft(
        data: CustomModel,
        position: Int,
        swipeDirection: Int
    ): Boolean {
        //return true if you want to delete on left or right swipe.
        return true
    }
}

Model class

class CustomModel(val firstName: String, val lastName: String)  

CustomViewHolder class

class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bindViews(customModel: CustomModel) {
        itemView.let {
            it.tv1.text = customModel.firstName
            it.tv2.text = customModel.lastName
        }
    }
}

This is just a simple adapter class. You just have to extend library's LazyRecyclerAdapter class. Use canSwapOrDrag(true) to enable the swipe and drag feature. Default is false.

Clone this wiki locally