The library that removes all boilerplate code allowing you to display lists with few lines of code.

Overview

VsRecyclerView

The library that removes all boilerplate code allowing you to display lists with few lines of code.

Gradle

androidExtensions {
    experimental = true //see https://kotlinlang.org/docs/tutorials/android-plugin.html#using-kotlin-android-extensions
}

dependencies {
    implementation 'com.wshtaits:vsrecyclerview:x.x.x' //see the latest version at https://github.com/wshtaits/VsRecyclerView/releases
}

Quick example

<com.wshtaits.vsrecyclerview.VsRecyclerView
    id="@+id/vs_recycler_view"
    layout_width="match_parent"
    layout_height="match_parent" />
//No need to write Adapters, ViewHolders or anything else. 
//Just:
vs_recycler_view.insertItems(names, R.layout.item_name) { name ->
    name_tv.text = name //'this' is item ViewHolder
}

Guide

Capabilities

Available methods with different parameters for various use cases:

class VsRecyclerView {

    fun setItem(...)
    fun setItems(...)
    fun setNoDataItem(...)
    fun setNoDataItems(...)
    
    fun insertItem(...)
    fun insertItems(...)
    fun insertNoDataItem(...)
    fun insertNoDataItems(...)
    
    fun insertItemToStart(...)
    fun insertItemsToStart(...)
    fun insertNoDataItemToStart(...)
    fun insertNoDataItemsToStart(...)
    
    fun insertItemToEnd(...)
    fun insertItemsToEnd(...)
    fun insertNoDataItemToEnd(...)
    fun insertNoDataItemsToEnd(...)
    
    fun changeToItem(...)
    fun changeToItems(...)
    fun changeToNoDataItem(...)
    fun changeToNoDataItems(...)
        
    fun removeItem(...)
    fun removeItems(...)
        
    fun moveItem(fromPosition: Int, toPosition: Int)
        
    fun clearItems()
}

Available parameters (consider insertItems(...) as an example):

class VsRecyclerView {
    ...
    fun <AdaptableData> insertItems(
        position: Int,
        dataCollection: Collection<AdaptableData>,
        adapter: ItemsAdapter<AdaptableData>
    )
    
    fun <AdaptableData> insertItems(
        position: Int,
        dataCollection: Collection<AdaptableData>,
        @LayoutRes itemLayoutResId: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.(AdaptableData) -> Unit
    )
    
    fun <AdaptableData> insertItems(
        position: Int,
        factoryFunction: (ViewGroup) -> View,
        @LayoutRes itemLayoutResId: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.(AdaptableData) -> Unit
    )
    
    fun insertNoDataItems(position: Int, adapter: NoDataItemsAdapter, count: Int)
    
    fun insertNoDataItems(
        position: Int,
        @LayoutRes itemLayoutResId: Int,
        count: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.() -> Unit = {}
    )
    
    fun insertNoDataItems(
        position: Int,
        factoryFunction: (ViewGroup) -> View,
        count: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.() -> Unit = {}
    )
    ...
}

ItemsAdapters

This is not about RecyclerView.Adapter, but about library ItemsAdapters. They are not creating alone for the entire RecyclerView, but for each type of element. Each time you don’t pass an ItemsAdapter explicitly, VsRecyclerView creates a new one with its own viewType (based on the hashCode() of the ItemsAdapter) and therefore with a separate ViewHolderPool. For example, two different adapters are creating here:

with(vsRecyclerView) {
    //creates 1st ItemsAdapter
    insertItems(names, R.layout.item_name) { name -> 
        name_tv.text = name
    }
    
    //creates 2nd ItemsAdapter
    insertItems(names, R.layout.item_name) { name -> 
        name_tv.text = name
    }
}

In such cases it's better to create an ItemsAdapter or template ItemsAdapter and pass it explicitly.

ItemsAdapter example:

class NamesAdapter : ItemsAdapter<String>(R.layout.item_contact) {

    //override optionally
    override fun ItemViewHolder.onCreateViewHolder() {
        print("onCreateViewHolder")
    }

    override fun ItemViewHolder.onBindViewHolder(data: String) {
        print("onBindViewHolder for $data")
    }
        
    //override optionally
    override fun getItemId(data: String): Long {
        return data.hashCode().toLong()
    }
}

Usage:

fun showNames(names: List<String>) {
    val namesAdapter = NamesAdapter()
    with(vsRecyclerView) {
        setItems(names, namesAdapter)
        insertNoDataItem(R.layout.divider)
        insertItem("Bob", namesAdapter)
    }
}

Template ItemsAdapters example:

class SimpleItemsAdapter<AdaptableData> {

    constructor(
        @LayoutRes layoutResId: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.(AdaptableData) -> Unit = { _ -> }
    )

    constructor(
        itemViewFactoryFunction: (parent: ViewGroup) -> View,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.(AdaptableData) -> Unit = { _ -> }
    )
}

class SimpleNoDataItemsAdapter {

    constructor(
        @LayoutRes layoutResId: Int,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.() -> Unit = {}
    )

    constructor(
        itemViewFactoryFunction: (parent: ViewGroup) -> View,
        onCreateAction: ItemViewHolder.() -> Unit = {},
        onBindAction: ItemViewHolder.() -> Unit = {}
    )
}

Usage:

fun showNames(names: List<String>) {
    val namesAdapter = SimpleItemsAdapter(R.layout.item_name) { name ->
        name_tv.text = name
    }
    with(vsRecyclerView) {
        setItems(names, namesAdapter)
        insertNoDataItem(R.layout.divider)
        insertItem("Bob", namesAdapter)
    }
}

ViewHolders

The library ViewHolder implements the LayoutContainer interface to cache views, so you need to reference the views through the ViewHolder instance, not through ViewHolder.itemView property. Read the article by Umut Uz.

Example:

//WRONG:
recyclerView.insertItems(names, R.layout.item_name) { name ->
    itemView.name_tv.text = name
}

//RIGHT:
recyclerView.insertItems(names, R.layout.item_name) { name ->
    name_tv.text = name
}

But if your xml layout contains only one view, then you can refer to it using the ViewHolder.itemView or ViewHolder.containerView properties:

recyclerView.insertItems("Title", R.layout.item_title) { title ->
    (itemView as TextView).text = title
}

Stable id's

If your items have stable ids, you need to specify this in the xml and override the getItemId(...) method in the ItemsAdapter or template ItemsAdapters.

<com.wshtaits.vsrecyclerview.VsRecyclerView
    layout_width="match_parent"
    layout_height="match_parent"
    hasStableIds="true"/>

Default layoutManager

LinearLayoutManager set by default. So if you need to use it, it is not necessary to specify it in xml.

Custom functions

If the methods provided are not enough for you, then add them using extension functions:

fun VsRecyclerView.insertName(name: String) {
    insertItem(name, R.layout.item_name) { name -> name_tv.text = name }
}

License

Copyright (c) 2019 Shtaits Valeriy.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

You might also like...
Square Cycler API allows you to easily configure an Android RecyclerView declaratively in a succinct way.

Square Cycler – a RecyclerView API The Square Cycler API allows you to easily configure an Android RecyclerView declaratively in a succinct way. Desig

Starter code for Android Kotlin Fundamentals Codelab 7.1 RecyclerView Fundamentals

TrackMySleepQuality with RecyclerView - Starter Code for 7.1 Starter code for Android Kotlin Fundamentals Codelab 7.1 RecyclerView Fundamentals Introd

An Android Animation library which easily add itemanimator to RecyclerView items.
An Android Animation library which easily add itemanimator to RecyclerView items.

RecyclerView Animators RecyclerView Animators is an Android library that allows developers to easily create RecyclerView with animations. Please feel

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)

Advanced RecyclerView This RecyclerView extension library provides Google's Inbox app like swiping, Play Music app like drag-and-drop sorting and expa

Android library providing simple way to control divider items (ItemDecoration) of RecyclerView
Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

RecyclerView-FlexibleDivider Android library providing simple way to control divider items of RecyclerView Release Note [Release Note] (https://github

Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Dividers is a simple Android library to create easy separators for your RecyclerViews
Dividers is a simple Android library to create easy separators for your RecyclerViews

Dividers Dividers is an Android library to easily create separators for your RecyclerViews. It supports a wide range of dividers from simple ones, tha

Android library defining adapter classes of RecyclerView to manage multiple view types
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Android pagination library (updated 01.05.2018)
Android pagination library (updated 01.05.2018)

NoPaginate Android pagination library, based on @MarkoMilos repository Paginate Loading Item Error Item Gradle implementation 'ru.alexbykov:nopaginate

Comments
  • How to get Position

    How to get Position

    1. How to get item position.
    2. How to do notifyDataSetChanged().
    3. without itemVeiw not working (ex. holder.tvid.text="test" not working showing error).
    opened by saecmca 1
Releases(v2.0.0)
Owner
Valeriy Shtaits
Valeriy Shtaits
Using RecyclerView to display data instead of ScrollView or lInearLayout for a strong app. It replaces the ScrollView used in trackMySleep app.

RecyclerView - SleepQualityTracker with RecyclerView app This is the toy app for Lesson 7 of the Android App Development in Kotlin course on Udacity.

Espérant GADA 0 Oct 18, 2021
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Shaima Alghamdi 3 Nov 18, 2021
ScrollableList - learn how to efficiently display a list of text in a RecyclerView and understand its architecture.

ScrollableList Learn how to efficiently display a list of text in a RecyclerView and understand its architecture. activity_main RecyclerView widget he

null 0 Jan 3, 2022
how to handle recyclerView in Kotlin to display grid , vertical , horizontal layouts

Dogglers - Starter Code Starter code for the second independent project for Android Basics in Kotlin. Introduction This is the starter code for the Do

null 0 Mar 28, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

null 313 Nov 12, 2022
A layout manager for the RecyclerView with interchangeable linear, grid, and staggered displays of views, all with configurable section headers including the sticky variety as specified in the material design docs.

SuperSLiM This is the version 5 development branch. Project Plan Support me on Patreon Blog What is Version 5 Version 5 is the current development bra

Tonic Artos 2.1k Jan 2, 2023
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps

Infinite Recycler View A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps. This library offers you a custom adapt

IB Sikiru 26 Dec 10, 2019
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Jack and phantom 504 Dec 25, 2022