• Stars
    star
    192
  • Rank 202,019 (Top 4 %)
  • Language
    Kotlin
  • Created about 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rx based RecyclerView Adapter

RxRecyclerAdapter

Release GitHub license Android Arsenal

Rx based generic RecyclerView Adapter Library.

How to use it?

Example!

  • Enable Databinding by adding these lines to your build.gradle
dataBinding {
      enabled = true
}
  • Create the layout file
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <TextView android:id="@+id/textViewItem"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  tools:text="Recycler Item"/>

    </LinearLayout>
</layout>
  • Create your dataSet
//Dummy DataSet
val dataSet = mutableListOf<String>()
dataSet.add("Lorem")
dataSet.add("ipsum")
dataSet.add("dolor")
dataSet.add("sit")
dataSet.add("amet")
  • Create RxDataSource
// Simple data source
val rxDataSource = RxDataSource<ItemLayoutBinding, String>(R.layout.item_layout, dataSet)

rxDataSource
        .map { it.toUpperCase() }
        .repeat(4)
        .asObservable()
        .subscribe {
            val binding = it.viewDataBinding ?: return@subscribe
            binding.textViewItem.text = it.item
        }

And that's it! The recyclerView is going to show

Changing the data dynamically

Simply call updateAdapter after making changes to the dataSet and that'll do the trick!

rxDataSource.map(...).filter(...).take(...).updateAdapter();

Adapter for multiple View Types

If multiple view types are required for your recyclerView, let's say, we have two types HEADER and ITEM then the coding steps will be :-

  • Enable Databinding
  • Create a list of ViewHolderInfo
//ViewHolderInfo List
val viewHolderInfoList = ArrayList<ViewHolderInfo>()
viewHolderInfoList.add(ViewHolderInfo(R.layout.item_layout, TYPE_ITEM))
viewHolderInfoList.add(ViewHolderInfo(R.layout.item_header_layout, TYPE_HEADER))
  • Create an instance of RxDataSourceSectioned implementation
 // Sectioned data source
val rxDataSourceSectioned = RxDataSourceSectioned(dataSet, viewHolderInfoList, object : OnGetItemViewType() {
    override fun getItemViewType(position: Int): Int {
        if (position % 2 == 0) { // even are headers
            return TYPE_HEADER
        }
        return TYPE_ITEM
    }
})
  • Compose and call bindRecyclerView passing in recyclerView, viewHolderInfoList and viewTypeCallBack
rxDataSourceSectioned
    .asObservable()
    .subscribe {
        val viewDataBinding = it.viewDataBinding
        val data = it.item

        when (viewDataBinding) {
            is ItemLayoutBinding -> viewDataBinding.textViewItem.text = "ITEM: " + data
            is ItemHeaderLayoutBinding -> viewDataBinding.textViewHeader.text = "HEADER: " + data
        }
    }

And the output would look something like

More examples and details here

How to update adapter?

You can update all the data set by call updateDataSet(), then call updateUdapter() to update the adapter (get "notifiyDataSetChange()" effect)

rxDataSource.updateDataSet(newDataSet)
rxDataSource.updateAdapter()

If you want to update one item of the data set, you can use updateDataSet(updatedList, effectedPosition, transactionType),

rxDataSource.updateDataSet(
  dataSet.apply { removeAt(deletedPosition) },
  deletedPosition,
  RxDataSource.TransactionTypes.DELETE
)

Download

Repository available on jCenter

implementation 'com.minimize.android:rxrecycler-adapter:1.3.2'

If the dependency fails to resolve, add this to your project repositories

repositories {
  maven {
      url  "http://dl.bintray.com/ahmedrizwan/maven"
  }
}

License

Copyright 2015 Ahmed Rizwan

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.

More Repositories

1

JetpackComposeCalculator

A jetpack compose clone of Android 10 Calculator UI
Kotlin
292
star
2

SeekBarCompat

A simple material-based support library to bring consistent SeekBars on Android 14 and above
Kotlin
157
star
3

JetpackComposeTwitter

A jetpack compose clone of Twitter UI
Kotlin
117
star
4

AndroidKotlinComponents

Boilerplates for Android Components Architecture with Rx, Dagger & Realm written in Kotlin
Kotlin
80
star
5

ChronometerPersist

A simple and easy to use Chronometer State helper library
Kotlin
66
star
6

RxRealmRetroKotlin

Example code demonstrating usage of Realm + Retrofit + Rx in Kotlin
Kotlin
61
star
7

ReactiveAudioRecord

Rx wrapper library for Android's AudioRecord API. (aka RxAudioRecord)
Java
32
star
8

andgen

Templates generator for Android :octocat:
Ruby
14
star
9

FunctionalKotlin-Demo

Demo code for Real World Functional Programming with Kotlin talk
Kotlin
7
star
10

angular-karma-test-action

Github action for running karma tests
HTML
3
star
11

XyzReader

Project 5 - Android Nanodegree
Java
2
star
12

ahmedrizwan

2
star
13

GoUbiquitous

Nanodegree - Project 6
Java
1
star
14

BuildItBigger

Project 4 - Android Nanodegree
Java
1
star
15

SpotifyStreamer

Udacity Project Spotify Streamer
Java
1
star
16

vs-code-bindings

Custom bindings to keep things a bit more consistent between Mac & Windows
1
star
17

homebrew-andgen

Ruby
1
star
18

Capstone-Project

Final Project for Nanodegree
Java
1
star
19

Lism-iOS

Lism App for iOS
Swift
1
star
20

HubRepos

Java
1
star
21

UdacityAppPortfolio

Udacity Project 0 - App Portfolio
Java
1
star
22

remix-infinite-scroll

A demo for doing infinite scroll loading in Remix.
TypeScript
1
star
23

vimrc

Vim configs (NeoVim and Idea)
Vim Script
1
star
24

SuperDuo

Project 3 - Android Nanodegree
Java
1
star
25

SimpleHub

A simple iOS swift app for getting user's github info
Swift
1
star
26

til

1
star
27

nvchad-custom-config

Lua
1
star