Skip to content

Instantly share code, notes, and snippets.

Created October 21, 2016 19:59
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save nightscape/23cd6fc45223afcc0ee892ca68012791 to your computer and use it in GitHub Desktop.
Save nightscape/23cd6fc45223afcc0ee892ca68012791 to your computer and use it in GitHub Desktop.
Simple RxJava-based adapter for an Android RecyclerView
public class ReactiveTextViewHolder<T> extends ReactiveViewHolder<T> {
private TextView label;
private T currentItem;
public ReactiveTextViewHolder(View itemView) {
label = (TextView) itemView.findViewById(;
public void setCurrentItem(T currentItem) {
this.currentItem = currentItem;
public T getCurrentItem() {
return currentItem;
ReactiveRecylerAdapter.ReactiveViewHolderFactory<String> viewAndHolderFactory = (parent, pViewType) -> {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
return new ReactiveRecylerAdapter.ReactiveViewHolderFactory.ViewAndHolder<>(
new ReactiveRecylerAdapter.ReactiveTextViewHolder<>(view)
ReactiveRecylerAdapter reactiveRecylerAdapter = new ReactiveRecylerAdapter(Observable.just("Here", "we", "go"), viewAndHolderFactory);
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jakewharton.rxbinding.view.RxView;
import java.util.Collections;
import java.util.List;
import rx.Observable;
import rx.subjects.PublishSubject;
public class ReactiveRecylerAdapter<T> extends RecyclerView.Adapter<ReactiveRecylerAdapter.ReactiveViewHolder<T>> {
private final Observable<List<T>> observable;
private final ReactiveViewHolderFactory<T> viewHolderFactory;
private List<T> currentList;
public ReactiveRecylerAdapter(Observable<List<T>> observable, ReactiveViewHolderFactory<T> viewHolderFactory) {
this.viewHolderFactory = viewHolderFactory;
this.currentList = Collections.emptyList();
this.observable = observable;
this.observable.observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
this.currentList = items;
private PublishSubject<T> mViewClickSubject = PublishSubject.create();
public Observable<T> getViewClickedObservable() {
return mViewClickSubject;
public ReactiveViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int pViewType) {
ReactiveViewHolderFactory.ViewAndHolder<T> viewAndHolder = viewHolderFactory.createViewAndHolder(parent, pViewType);
ReactiveViewHolder<T> viewHolder = viewAndHolder.viewHolder;
.map(aVoid -> viewHolder.getCurrentItem())
return viewHolder;
public void onBindViewHolder(ReactiveViewHolder<T> holder, int position) {
T item = currentList.get(position);
public int getItemCount() {
return currentList.size();
public static abstract class ReactiveViewHolder<T> extends RecyclerView.ViewHolder {
public ReactiveViewHolder(View itemView) {
public abstract void setCurrentItem(T t);
public abstract T getCurrentItem();
public interface ReactiveViewHolderFactory<T> {
class ViewAndHolder<T> {
public final View view;
public final ReactiveViewHolder<T> viewHolder;
public ViewAndHolder(View view, ReactiveViewHolder<T> viewHolder) {
this.view = view;
this.viewHolder = viewHolder;
ViewAndHolder<T> createViewAndHolder(@NonNull ViewGroup parent, int pViewType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment