Android library for RecyclerView to manage order of items and multiple view types.

Overview

recyclerview-binder

License Android Arsenal Download

Android Library for RecyclerView to manage order of items and multiple view types.

Features

  • Insert any items to wherever you want without calculating their position.
  • Insert any items in desired order regardless of the order of calling 'add/insert'.
  • Separate implementations for each view type into their own classes.
  • Support RxJava.

Sample

Sample

Gradle

repositories {
    jcenter()
}
dependencies {
    compile 'jp.satorufujiwara:recyclerview-binder:1.3.2'
}

Quick Start

ViewType decides RecyclerView item's layout.

public enum BinderSampleViewType implements ViewType {

    VIEW_TYPE_1,
    VIEW_TYPE_2,
    VIEW_TYPE_3;

    @Override
    public int viewType() {
        return ordinal();
    }
}

Binder binds data to Views in RecyclerView item.

public class DataBinder1 extends RecyclerBinder<BinderSampleViewType> {

    private final String text;

    public DataBinder1(Activity activity, String text) {
        super(activity, BinderSampleViewType.VIEW_TYPE_1);
        this.text = text;
    }

    @Override
    public int layoutResId() {
        return R.layout.binder_data_1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.textSection.setText(text);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textSection;
        public ViewHolder(View view) {
            super(view);
            textSection = (TextView) view.findViewById(R.id.text_section);
        }
    }
}

Binder added to Adapter is layouted in order of Section.

    RecyclerBinderAdapter<BinderSampleSection, BinderSampleViewType> adapter
            = new RecyclerBinderAdapter<>();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        adapter.add(BinderSampleSection.SECTION_1, new DataBinder1(getActivity(), "in Section1"));
        adapter.add(BinderSampleSection.SECTION_1, new DataBinder2(getActivity(), "in Section1"));
        adapter.add(BinderSampleSection.SECTION_1, new DataBinder3(getActivity(), "in Section1"));

        adapter.add(BinderSampleSection.SECTION_3, new DataBinder2(getActivity(), "in Section3"));
        adapter.add(BinderSampleSection.SECTION_3, new DataBinder1(getActivity(), "in Section3"));

        adapter.add(BinderSampleSection.SECTION_2, new DataBinder3(getActivity(), "in Section2"));
        adapter.add(BinderSampleSection.SECTION_2, new DataBinder2(getActivity(), "in Section2"));
        adapter.add(BinderSampleSection.SECTION_2, new DataBinder1(getActivity(), "in Section2"));
    }

    enum BinderSampleSection implements Section {

        SECTION_1,
        SECTION_2,
        SECTION_3;
        
        @Override
        public int position() {
            return ordinal();
        }
    }

Usage

1. Create class implemented ViewType

Enum is useful for this class.

2. Create classes extends RecyclerBinder

The number of these classes is the same to number of ViewType in Step 1.

3. Create class implemented Section

Enum is useful for this class.

4. Create RecyclerBinderAdapter

RecyclerBinderAdapter<Section, ViewType> adapter = new RecyclerBinderAdapter<>();

5. Control RecyclerView

adapter.add(Section.SECTION_1, binder);
adapter.addIfEmpty(Section.SECTION_1, binder);
adapter.addAll(Section.SECTION_1, List<RecyclerBinder> binders);
adapter.insert(Section.SECTION_1, binder, index);
adapter.remove(Section.SECTION_1, binder);
adapter.removeAll(Section.SECTION_1);
adapter.replaceAll(Section.SECTION_1, List<RecyclerBinder> binders);
adapter.clear();

Use with RxJava

If subscibe observable in RecyclerBinder classes, use RxRecyclerBinder and bindToLifecycle().

myObservable
    .compose(bindToLifecycle())
    .subscribe();

When item removed from adapter, observable unsubscribe. Note : Due to the unsubscription works, please call RecyclerBinderAdapter.clear() in Activity.onDestroy() or Fragment.onDestroyView().

License

Copyright 2015 Satoru Fujiwara

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...
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

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.

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

RecyclerView : SleepQualityTracker with RecyclerView app
RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView - SleepQualityTracker with RecyclerView app SleepQualityTracker with RecyclerView This app builds on the SleepQualityTracker developed pr

Dogglers app uses a RecyclerView in horizontal, vertical and grid view to show a group of CardViews
Dogglers app uses a RecyclerView in horizontal, vertical and grid view to show a group of CardViews

Welcome to DogglersApp! This app is the final project from Unit 2 of Android Basics in Kotlin given by Google codelabs: https://developer.android.com/

Handy library to integrate pagination, which allow no data layout, refresh layout, recycler view in one view and easy way to bind pagination in app.
Handy library to integrate pagination, which allow no data layout, refresh layout, recycler view in one view and easy way to bind pagination in app.

Pagination View Handy library to integrate pagination, which allow no data layout, refresh layout, recycler view in one view and easy way to bind pagi

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

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

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

Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.

xAdapter: Kotlin DSL 风格的 Adapter 封装 1、简介 该项目是 KotlinDSL 风格的 Adapter 框架封装,用来简化 Adapter 调用,思想是采用工厂和构建者方式获取 Adapter 避免代码中定义大量的 Adapter 类。该项目在 BRVAH 的 Ada

Comments
  • duplicate entry: android/support/compat/R.class

    duplicate entry: android/support/compat/R.class

    When using the latest version 2.0.0 compilation breaks:

    • What went wrong: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.

    com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/compat/R.class

    In my dependencies I have

    compile 'com.android.support:appcompat-v7:26.0.+'
    compile 'com.android.support:recyclerview-v7:26.0.+'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
    compile 'com.google.android.gms:play-services-vision:11.0.4'
    compile 'jp.satorufujiwara:recyclerview-binder:1.3.3'
    
    opened by slott 2
  • Divide rx-java

    Divide rx-java

    I update RxJava2.x from 1.x and divide RxJava project. Please release it as rx-java plugin.

    I referred to RxLifecycle.

    You can dispose like this

    Observable.just(10)
        .compose(bindToLifecycle()) // bind to binder's lifecycle
        .subscribe();
    
    opened by AAkira 0
  • Item order is messed up

    Item order is messed up

    I have implemented this library in a RecyclerView and its working smooth. I get a List of parse objects from parse which is sorted chronologically. The only problem i face is that when i push this list of parse objects to the adapter it goes on order. For some reason though, it is displayed without a chronological order in the RecyclerView. This adapter is somehow not maintaining the correct order. Can you please tell me if its my problem or its a problem in the library that the order is not saved.

    opened by kristoprifti 0
Releases(2.0.2)
Owner
Satoru Fujiwara
Developer Relations of @LINE
Satoru Fujiwara
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

Daichi Furiya 11.2k Jan 8, 2023
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

Yoshihito Ikeda 2.4k Dec 18, 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
Examples of custom recycler view items. Automatically detecting a dominant color of an image using Picasso and Palette libraries

custom-image-list-item Examples of custom RecyclerView items using Data Binding Features: Loading images urls with the help of a Picasso library Autom

Alina Stepanova 2 Sep 12, 2022
Shimo is an adapter for Moshi which randomizes the order of keys when serializing and deserializing

Shimo Shimo is a JsonAdapter.Factory for Moshi which randomizes the order of keys when serializing objects to JSON and when deserializing objects from

Jake Wharton 179 Dec 19, 2022
TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.

TikTok-RecyclerView Demo About This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoP

Baljeet Singh 19 Dec 28, 2022
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 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
Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

Rakshit Nawani 0 Jan 3, 2022