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
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

ShouHeng 17 Oct 9, 2022
Don't write a RecyclerView adapter again. Not even a ViewHolder!

LastAdapter Don't write a RecyclerView adapter again. Not even a ViewHolder! Based on Android Data Binding Written in Kotlin No need to write the adap

Miguel Ángel Moreno 781 Dec 19, 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
kotlin dsl for kids to simplify RecyclerView.Adapter logic

KidAdapter RecyclerView adapter for kids. A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter. With KidAdapter y

Eugeniu Tufar 56 Nov 27, 2022
A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps

Infinite Recycler View A RecyclerView Adapter which allows you to have an Infinite scrolling list in your apps. This library offers you a custom adapt

IB Sikiru 26 Dec 10, 2019
Reproducible sample with Fix for Memory Leak in RecyclerView Adapter

Memory Leak RecyclerView Adapter Reproducible Sample with Fix Video Instructions: https://www.youtube.com/c/awesomedevnotes Code Only the relevant and

Awesome Dev Notes | Android Dev Notes YouTube 7 Jun 7, 2022
RecyclerView With No Adapter | Available For Jetpack Compose

About This Project Available on Google Dev Library Click Here RecyclerView No Adapter (Adapter Has Been Handled) RecyclerView No Adapter Using ViewBin

Faisal Amir 142 Oct 18, 2022
Easy RecyclerView Adapter

GenericAdapter Easy RecyclerView Adapter Getting started build.gradle allprojects { repositories { // ... maven { url 'https://jit

JaredDoge 4 Dec 3, 2021
Add RecyclerView, use Adapter class and ViewHolder to display data.

فكرة المشروع في هذا المشروع سنقوم بعرض قائمة من البيانات للطلاب على واجهة تطبيق Android بإستخدام: مفهوم RecyclerView مفهوم Adapter مفهوم ViewModel محت

Shaima Alghamdi 3 Nov 18, 2021
Just another one easy-to-use adapter for RecyclerView :rocket:

Elementary RecyclerView Adapter Another one easy-to-use adapter for RecyclerView ?? Features: DSL-like methods for building adapters similar to Jetpac

Roman Andrushchenko 29 Jan 6, 2023
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

Oleg Beloy 3.2k Dec 25, 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
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.

Jack and phantom 504 Dec 25, 2022
RecyclerView : SleepQualityTracker with RecyclerView app

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

Kevin 2 May 14, 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
Yet another adapter delegate library.

Yet another adapter delegate library. repositories { ... maven { url 'https://jitpack.io' } } ... dependencies { implementation("com.git

Mike 10 Dec 26, 2022