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

Overview

RecyclerView-MultipleViewTypeAdapter

Android Arsenal License Download

RecyclerView adapter classes for managing multiple view types

Sample

Release Note

[Release Note] (https://github.com/yqritc/RecyclerView-MultipleViewTypesAdapter/releases)

Gradle

repositories {
    jcenter()
}

dependencies {
    compile 'com.yqritc:recyclerview-multiple-viewtypes-adapter:1.1.0'
}

About this library

The standard way to handle view creation and data binding logics for RecyclerView is to define the single adapter class extending RecyclerView.Adapter.
If you need to handle simple view type, you will not get in trouble.
But if you need to handle the multiple complex view types, writing logic in single adapter class causes unmaintainable source code.
So, this library separates the logic for each view type into single class (DataBinder), and change the role of adapter class (DataBindAdapter) to manage these DataBinder classes.
By using this library, the implementation would be simple and clear, and also source code for each view type would be maintenable and reusable.

Usage

1. Create DataBinder

Define class extending DataBinder for each view type.
DataBinder class is used to bind view and data.

Please refer the binder samples for usage.

2. Create DataBindAdapter

Create adapter class to manage DataBinder classes.
Use (or extend) ListBindAdapter (or EnumListBindAdapter) if the order of view types used for recyclerview is in sequence. If the order of view types is complex, create class by extending EnumMapBindAdapter or DataBindAdapter.

Please refer the adapter samples for usage.

3. Set adapter for recyclerview

License

Copyright 2015 yqritc

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.
Comments
  • Add new binder dynamically

    Add new binder dynamically

    is it possible to add a new binder dynamically after already initializing EnumListBindAdapter and using addAllBinder in the constructor? I have a different row type that i want to add if a certain condition is met, and removed if not.

    opened by silviahisham 2
  • Error when trying to clear items in the binder

    Error when trying to clear items in the binder

    I get this error when using clear() in Sample2Binder

    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{4266fe38 position=2 id=-1, oldPos=2, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
    
    opened by silviahisham 2
  • wrong position for getPosition(DataBinder binder, int binderPosition)

    wrong position for getPosition(DataBinder binder, int binderPosition)

    //TYPE1
    public void bindViewHolder(ViewHolder holder, int position) {
                Log.w("bindViewHolder",position+" "+getPosition(this,position));
    }
    

    0 1 1 1 2 2 3 3 should be: 0 1 1 2 2 3 3 4

    data example: TYPE0 TYPE1 TYPE1 TYPE1

    in getPosition

                    --binderPosition;
                    if(binderPosition <= 0) {
    

    when positionOfBinder==0, after1 loop, -1<=0 so return 1 (correct) when positionOfBinder==1, after1 loop, 0<=0 so return 1 (wrong, it should be 2)

    i think binderPosition should not <0, so i fix it with

                    if(binderPosition-- <= 0) {
    

    when positionOfBinder==0, after1 loop, 0<=0 so return 1 (correct) when positionOfBinder==1, first loop 1<=0 is false , second loop, 0<=0 so return 2 (correct)

    opened by chouex 2
  • two dynamic lists

    two dynamic lists

    Does this library support two dynamic lists? For example if I have two different binders that each have a dynamic list, would I need to do anything differently to get it to work?

    Currently I'm getting indexing issues when using ListBindAdapter. What's the best way to do this? thanks in advance

    opened by narinearraul 2
  • ClassCastException: A$ViewHolder cannot be cast to B$ViewHolder

    ClassCastException: A$ViewHolder cannot be cast to B$ViewHolder

    Hey,

    I am using setBinder(0, newBinder) to replace the first item on my adapter (ListBindAdapter), and then called notifyBinderItemRangeChanged(newBinder, 0, 1). It is unable to replace the first DataBinder.

    It crashed at com.yqritc.recyclerviewmultipleviewtypesadapter.DataBindAdapter.onBindViewHolder(DataBindAdapter.java:21)

    opened by winsonlim 2
  • Clever notifyBinderDataSetChanged

    Clever notifyBinderDataSetChanged

    Hi,

    I'm implementing a clever approach to notifyBinderDataSetChanged - so that calling this will automatically figure out if it should add/remove or update the items.

    Would you be interested in a PR once I get this working or you see this as besides the scope of this lib ?

    User case here would be that users can simply add/remove/update items in a binder and simply call notifyBinderDataSetChanged and not having to worry about what actions they took.

    opened by slott 1
  • Cannot be cast to ...

    Cannot be cast to ...

    Every now and then I end up getting a class cast exception such as:

    java.lang.ClassCastException: com.vivino.android.marketsection.binders.ComputationalTopListsBinder$ViewHolder cannot be cast to com.vivino.android.marketsection.binders.HighlightedOfferBinder$ViewHolder

    Seems its picking the wrong binder at the wrong time and then things go south...

    This happens only when I use my custom adapter with support for removeBinder - So not really your wrong doing..

    opened by slott 1
  • Modify 'for' loops performance sensitively

    Modify 'for' loops performance sensitively

    Thank you very much for such an amazing library! I have made tiny modification here to make the library performance better just a tiny bit..

    Done

    I have modified for loops inside EnumMapBindAdapter and ListBindAdapter to calculate initial list size only once. I have decided to make this modification after seeing this youtube video: https://www.youtube.com/watch?v=b6zKBZcg5fk&feature=youtu.be&t=1537

    opened by shoheikawano 1
  • getItemViewType in EnumMapBindAdapter

    getItemViewType in EnumMapBindAdapter

    What does getITemViewType do in EnumMapBindAdapter exactly? and what if I have one arraylist of different object types and not separate lists, how can I change view types based on object type?, if I use separate binders then i have to have separate lists, so I tried using one binder and override getItemViewType in it, but turned out I can't since it extends ViewHolder.

    opened by silviahisham 1
  • Manage 4 Binders

    Manage 4 Binders

    I am using EnumMapBindAdapter to display userposts list .Here I am using 4 binders ,each binder has a list of size 3 . I have problem with getEnumFromPosition() ,According to my assumptions number of executions of getEnumFromPosition() method should be equal to number of binders (4) . But it is not happening .I have total 4 binders and I assume getEnumFromPosition() method should execute 4 times only (0,1,2,3) ,but executions are being excedeed . My assumption may be wrong ,If I am wrong then how can I manage 4 binders inside getEnumFromPosition() for different positions . If I put else section then it will return a single view when position exceeds .

    Here is the code of SampleEnumMapAdapter.java

    public class SampleEnumMapAdapter extends EnumMapBindAdapter<SampleEnumMapAdapter.SampleViewType> {

    enum SampleViewType{
        TEXT,IMAGE_AND_TEXT, AUDIO_AND_TEXT, VIDEO_AND_TEXT
    }
    
    public SampleEnumMapAdapter(){
        putBinder(SampleViewType.TEXT, new TextBinder(this));
        putBinder(SampleViewType.IMAGE_AND_TEXT, new ImageAndTextBinder(this));
        putBinder(SampleViewType.AUDIO_AND_TEXT, new AudioAndTextBinder(this));
        putBinder(SampleViewType.VIDEO_AND_TEXT, new VideoAndTextBinder(this));
    }
    
    public void setData(List<TextData> dataSetText ,List<ImageAndTextData> dataSetTextAndImage ,List<AudioAndTextData> dataSetTextAndAudio ,List<VideoAndTextData> dataSetTextAndVideo ) {
        ((TextBinder) getDataBinder(SampleViewType.TEXT)).addAll(dataSetText);
        ((ImageAndTextBinder) getDataBinder(SampleViewType.IMAGE_AND_TEXT)).addAll(dataSetTextAndImage);
        ((AudioAndTextBinder) getDataBinder(SampleViewType.AUDIO_AND_TEXT)).addAll(dataSetTextAndAudio);
        ((VideoAndTextBinder) getDataBinder(SampleViewType.VIDEO_AND_TEXT)).addAll(dataSetTextAndVideo);
    }
    
    @Override
    public SampleViewType getEnumFromPosition(int position){
        if (position == 0) {
            return SampleViewType.TEXT;
        } else if (position == 1) {
            return SampleViewType.IMAGE_AND_TEXT;
        } else if(position == 2){
            return SampleViewType.AUDIO_AND_TEXT;
        }else if(position == 3){
            return SampleViewType.VIDEO_AND_TEXT;
        }
        return null;
    }
    
    @Override
    public SampleViewType getEnumFromOrdinal(int ordinal) {
        return SampleViewType.values()[ordinal];
    }
    

    }

    opened by ersps25 1
  • how to implements StaggeredGridLayoutManager with the multipleViewTypesAdapter

    how to implements StaggeredGridLayoutManager with the multipleViewTypesAdapter

    thank you for your great multipleViewTypesAdapter.

    but when i used it wanting to show a StaggeredGrid UI, i found something undesirably.

    it can not show staggerGrid but the different children has the same height .

    So how to implement StaggeredGrid with multipleViewTypesAdapter,thanks a lot.

    as my code: MainActivity.java

    onCreate(Bundle savedInstanceState) { ... RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_main); SampleEnumListAdapter adapter = new SampleEnumListAdapter(); recyclerView.setAdapter(adapter); // recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 瀑布流 recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); // 自定义分割线 // SpacesItemDecoration decoration = new SpacesItemDecoration(this, // SpacesItemDecoration.VERTICAL_LIST); // recyclerView.addItemDecoration(decoration); // -----------// List sampleData = getSampleData(); adapter.setSample2Data(sampleData); ...

    }

    and the different binder

    Sample1Binder.java

    @Override public void bindViewHolder(ViewHolder holder, int position) { ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数 params.height = 50;// 把随机的高度赋予item布局 holder.itemView.setLayoutParams(params);// 把params设置给item布局

        Picasso.with(holder.mImageView.getContext())
        .load(R.drawable.bird)
        .into(holder.mImageView);
    }
    

    Simple2Binder.java

    @Override public void bindViewHolder(ViewHolder holder, int position) { ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数 params.height = 100;// 把随机的高度赋予item布局 holder.itemView.setLayoutParams(params);// 把params设置给item布局

        SampleData data = mDataSet.get(position);
        holder.mTitleText.setText(data.mTitle);
        holder.mContent.setText(data.mContent);
        holder.mImageView.setImageResource(R.drawable.animal1);
    
    }
    

    Sample3Binder.java

    public void bindViewHolder(ViewHolder holder, int position) { ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();// 得到item的LayoutParams布局参数 params.height = 150;// 把随机的高度赋予item布局 holder.itemView.setLayoutParams(params);// 把params设置给item布局

        holder.mImageView.setImageResource(R.drawable.animal3);
    
    }
    
    opened by leonzhuhi 1
Releases(v1.1.0)
Owner
Yoshihito Ikeda
Yoshihito Ikeda
An adapter which could be used to achieve a parallax effect on RecyclerView.

android-parallax-recycleview Integration Step 1. Add the JitPack repository to your build file repositories { maven { url "https://jitpack

Pedro Oliveira 1.6k Nov 17, 2022
An Android staggered grid view which supports multiple columns with rows of varying sizes.

AndroidStaggeredGrid ##Notice - Deprecated - 09-2015 This library has been deprecated. We will no longer be shipping any updates or approving communit

Etsy, Inc. 4.8k Dec 29, 2022
. Android library that integrate sticky section headers in your RecyclerView

recyclerview-stickyheaders Recyclerview-stickyheaders is an Android library that makes it easy to integrate section headers in your RecyclerView. Thes

null 968 Nov 10, 2022
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 5, 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
*** WARNING: This library is no longer maintained *** An easy way to add a simple 'swipe-and-do-something' behavior to your `RecyclerView` items. Just like in Gmail or Inbox apps.

SwipeToAction An easy way to add a simple 'swipe-and-do-something' behavior to your RecyclerView items. Just like in Gmail or Inbox apps. Integration

Victor Calvello 223 Nov 16, 2022
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

Haruki Hasegawa 5.2k Dec 23, 2022
ItemDecoration for RecyclerView using LinearLayoutManager for Android

RecyclerItemDecoration RecyclerItemDecoration allows you to draw divider between items in recyclerview with multiple ViewType without considering item

magiepooh 328 Dec 27, 2022
[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView

This project is no longer being maintained sticky-headers-recyclerview This decorator allows you to easily create section headers for RecyclerViews us

timehop 3.7k Dec 31, 2022
Pumped up RecyclerView

##Description This is an attempt to make RecyclerView easier to use. Features built in: ProgressBar while adapter hasn't been set EmptyView if adapter

Anton Malinskiy 2.6k Jan 5, 2023
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Nov 23, 2022
A Fast Scroller for the RecyclerView world!

RecyclerViewFastScroller The RecyclerViewFastScroller is a widget that can be added to a layout and connected to a RecyclerView for fast scrolling. Th

Daniel Smith 1.1k Dec 19, 2022
A drag-and-drop scrolling grid view for Android

DraggableGridView¶ ↑ a drag-and-drop scrolling grid view for Android Including in your project¶ ↑ To start using DraggableGridView: Place libs/Draggab

Tom Quinn 565 Dec 27, 2022
Android library to display a ListView whose cells are not rigid but flabby and react to ListView scroll.

FlabbyListView This library is not maintained anymore and there will be no further releases Android library to display a ListView which cells are not

JPARDOGO 762 Nov 23, 2022
An android library for section headers that stick to the top

StickyListHeaders StickyListHeaders is an Android library that makes it easy to integrate section headers in your ListView. These section headers stic

Emil Sjölander 5.5k Jan 2, 2023
Android library to achieve in an easy way, the behaviour of the home page in the Expedia app, with a pair of auto-scroll circular parallax ListViews.

ListBuddies This library is not maintained anymore and there will be no further releases Android library of a pair of auto-scroll circular parallax Li

JPARDOGO 970 Dec 29, 2022
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

Soichiro Kashima 9.6k Dec 29, 2022
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

Karumi 490 Dec 28, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022