A generic recycler view adapter

Overview

KRecyclerViewAdapter

platform API License: MIT Language: JAVA Twitter

A generic recyclerview adapter for all your recycler views.
No need to create a custom adapter for every recyclerview.

Dependency:

Add the following in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency in your app level gradle:

implementation 'com.github.KalpeshTalkar:KRecyclerViewAdapter:1.0.0'

Usage:

Extend your holder from KRecyclerViewHolder

Refer the sample code below:

public class MyHolder extends KRecyclerViewHolder {

    private TextView titleLabel, descLabel;

    public MyHolder(View itemView) {
        super(itemView);
        titleLabel = (TextView) itemView.findViewById(R.id.titleLabel);
        descLabel = (TextView) itemView.findViewById(R.id.descLabel);
    }
    
    @Override
    protected void setSelected(@NonNull Context context, boolean selected) {
        super.setSelected(context, selected);
        // This method is called whenever the holder is selected/unselected.
        if (selected) {
            // Selected
        } else {
            // Unselected
        }
    }

    @Override
    protected void setData(@NonNull Context context, @NonNull Object itemObject) {
        super.setData(context, itemObject);
        // This method is called automatically by the adapter.
        // override this method and set your data here...
        // Check the type of itemObject
        if (itemObject instanceof MyObject) {
            MyObject myObject = (MyObject)itemObject;
            titleLabel.setText(myObject.title);
            descLabel.setText(myObject.description);
        }
    }
    
}

Set your adapter (Single View Type)

KRecyclerViewAdapter adapter = new KRecyclerViewAdapter(this, YOUR_ARRAY, new KRecyclerViewHolderCallBack() {
    @Override
    public KRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item, parent, false);
        return new MyHolder(layoutView);
    }

    @Override
    public void onHolderDisplayed(@NonNull KRecyclerViewHolder holder, int position) {
        Log.i("onHolderDisplayed", "Holder Displayed At: " + position);
    }       
}, new KRecyclerViewItemClickListener() {
    @Override
    public void onRecyclerItemClicked(@NonNull KRecyclerViewHolder holder, @NonNull Object itemObject, int itemPosition) {
        Toast.makeText(MainActivity.this, "Clicked position " + itemPosition, Toast.LENGTH_SHORT).show();
    }
});

YOUR_RECYCLER_VIEW.setAdapter(adapter);

Other properties

adapter.allowsSingleSelection = true;           // Enables single selection
adapter.allowsMultipleSelection = true;         // Enables multiple selection
adapter.deselectItemOnClickIfSelected = true;   // Deselects the item if already selected.

adapter.getSelectedIndexes();                   // Get list of selected item positions

Set your adapter (Multiple View Type)

KRecyclerViewAdapter adapter = new KRecyclerViewAdapter(this, YOUR_ARRAY, new KRecyclerViewHolderViewTypeCallBack() {
    @Override
    public int recyclerItemViewType(int position, @NonNull Object itemObject) {
        if (position % 2 == 0) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public KRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item, null);
            return new SimpleHolder(layoutView);
        } else {
            View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.another_item, null);
            return new AnotherHolder(layoutView);
        }
    }

    @Override
    public void onHolderDisplayed(@NonNull KRecyclerViewHolder holder, int position) {
        Log.i("onHolderDisplayed", "Holder Displayed At: " + position);
    }
}, new KRecyclerViewItemClickListener() {
    @Override
    public void onRecyclerItemClicked(@NonNull KRecyclerViewHolder holder, @NonNull Object itemObject, int itemPosition) {
        Toast.makeText(MainActivity.this, "Clicked position " + itemPosition, Toast.LENGTH_SHORT).show();
    }
});

YOUR_RECYCLER_VIEW.setAdapter(adapter);
You might also like...
A generic network layer written in swift

SwiftyNet 1.0.0 A generic network layer written in swift. you can use it as an abstraction layer above Alamofire with generic returned types. Installa

An Generic HTTP Request Library For Swift

GRequest An HTTP request library written in Swift. ##Basic Usage Be simple, as it should be: Request("https://api.github.com/repos/lingoer/SwiftyJSON/

Tech blog about Put Generic Protocol as Variable Type. How Combine Publisher put into AnyPublisher

How to Put Generic Protocol as Variable Type Have you ever put a Protocol on a variable? I believe you do. The Delegate pattern is using a lot in UIKi

Generic Network Layer created using Swift.

Generic-Network-Layer_iOS Generic Network Layer created using URLSession. Networking is an essential element in app development, and you'll need API c

A lightweight generic cache for iOS written in Swift with extra love for images.
A lightweight generic cache for iOS written in Swift with extra love for images.

Haneke is a lightweight generic cache for iOS and tvOS written in Swift 4. It's designed to be super-simple to use. Here's how you would initalize a J

A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.

QuickDB FileManager + CoreData ❗️ Save and Retrieve any thing in JUST ONE line of code ❗️ Fast usage dataBase to avoid struggling with dataBase comple

A Generic Priority Queue in Pure Swift

SwiftPriorityQueue SwiftPriorityQueue is a pure Swift (no Cocoa) implementation of a generic priority queue data structure, appropriate for use on all

This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and AppleTV app.
This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and AppleTV app.

This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and Apple TV app. With this Framework you can create iPh

AsyncHTTP - Generic networking library written using Swift async/await

Generic networking library written using Swift async/await

A Github action for creating generic run report using Markdown
A Github action for creating generic run report using Markdown

create-report A Github action for creating generic run report (using Markdown!) - uses: michaelhenry/[email protected] with: report-title: "

A lightweight generic networking API written purely in Swift

SwiftyNetworking SwiftyNetworking library is a generic networking library writte

A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.
A generic small reusable components for data source implementation for UITableView/UICollectionView in Swift.

GenericDataSource A generic small reusable components for data source implementation for UITableView/UICollectionView written in Swift. Features Basic

A generic stretchy header for UITableView and UICollectionView

GSKStretchyHeaderView, by gskbyte GSKStretchyHeaderView is an implementation of the stretchy header paradigm as seen on many apps, like Twitter, Spoti

UI Kit for the STREAMD anime app. Generic UI objects go here, nothing to do with anime

STREAMDUIKit UI Kit for the STREAMD anime app. Generic UI objects go here, nothi

A generic state manager compatible with Combine

Loadable A generic state manager Loadable is a simple state manager that helps you to better handle asynchronous operations How it works You can deliv

SwiftExtensionKit - SwiftExtensionKit is to contain generic extension helpers for UIKit and Foundation

RichAppz PureSwiftExtensionKit SwiftExtensionKit is to contain generic extension

Generic model framework

Pistachio Pistachio is a generic model framework. By leveraging lenses and value transformers, it allows you to create type safe adapters for any recu

Type-Erased Existential Generic AsyncSequence Values in Swift

AnyAsyncSequence AnyAsyncSequence allows you to expose AsyncSequence interfaces in your APIs without exposing the underlying sequence type, while cont

HotCoffee is learning project with MVVM, Generic,Swift 5, TableView

HotCoffee A simple Coffee Ordering app built using TableView, MVVM design pattern and Swift5. Tools Xcode Version 13.2.1 Framework -UIKit Architecture

Releases(1.0.0)
Owner
Kalpesh Talkar
Kalpesh Talkar
Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman)

RequestAdapter Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman) The code is taken from:

Mihaela Mihaljevic Jakic 0 Nov 22, 2021
Swift library for working with Debug Adapter Protocol (DAP)

DebugAdapterProtocol This is a Swift library for interacting with Debug Adapter Protocol. It contains type definitions and utilities useful for both s

Chime 2 Apr 12, 2022
PostgreSQL database adapter (ORM included)

PostgreSQL PostgreSQL adapter for Swift 3.0. Conforms to SQL, which provides a common interface and ORM. Documentation can be found there. Installatio

Zewo Graveyard 91 Sep 9, 2022
:droplet: A generic view model for both basic and complex scenarios

Brick Description Brick is a generic view model for both basic and complex scenarios. Mapping a basic table view cells is as easy as pie, if you have

HyperRedink 59 Jul 31, 2021
Generic table view controller with external data processing

FlexibleTableViewController Swift library of generic table view controller with external data processing of functionality, like determine cell's reuse

Dmytro Pylypenko 9 May 20, 2018
Generic collection view controller with external data processing

FlexibleCollectionViewController Swift library of generic collection view controller with external data processing of functionality, like determine ce

Dmytro Pylypenko 3 Jul 16, 2018
🛶Shallows is a generic abstraction layer over lightweight data storage and persistence.

Shallows Shallows is a generic abstraction layer over lightweight data storage and persistence. It provides a Storage<Key, Value> type, instances of w

Oleg Dreyman 620 Dec 3, 2022
Rx is a generic abstraction of computation expressed through Observable Element interface,

Rx is a generic abstraction of computation expressed through Observable<Element> interface, which lets you broadcast and subscribe to values and other events from an Observable stream.

ReactiveX 23.1k Dec 31, 2022
A lightweight generic cache for iOS written in Swift with extra love for images.

Haneke is a lightweight generic cache for iOS and tvOS written in Swift 4. It's designed to be super-simple to use. Here's how you would initalize a J

Haneke 5.2k Dec 11, 2022
Generic Cross Platform Signal Handler

Signals Generic Cross Platform Signal Handler. Prerequisites Swift Swift Open Source swift-4.0.0-RELEASE toolchain (Minimum REQUIRED for latest releas

Kitura 91 Oct 2, 2022