Easy RecyclerView Adapter

Overview

GenericAdapter

Easy RecyclerView Adapter

Getting started

build.gradle

allprojects {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
	        implementation 'com.github.JaredDoge:GenericAdapter:{latest_version}'
	}

Usage

   recyclerview.layoutManager = LinearLayoutManager(context)

   adapter= GenericAdapter(list)
            .addBinder(R.layout.adapter_item1){
                onBindView { holder, view, pos, data, payloads ->
                    val tv:TextView = holder.find(R.id.tv)
                    tv.text=data.msg
                }
            }
            .addItemClick { view, position, data ->
                  //do someing
            }
 
   recyclerview.adapter=adapter

MultiType

SampleModel

sealed class Data {

    data class Type1(val t1: String) : Data()

    data class Type2(val t2: String) : Data()
}

Adaptaer

 adapter= GenericAdapter(list)
            .setTypeCondition { pos, obj ->
            
                //如果為multiType 則必須呼叫,Generic才能判斷type
                //這裡的type 對應你的TypeBinder 裡的 getViewType()
                
                if(obj is Data.Type1) 1 else 2
            }
            .addTypeBinder<Data.Type1>(R.layout.adapter_item1){
                onBindView { holder, view, pos, data, payloads ->
                   //type 1 bind view
                   val v1= holder.find<TextView>(R.id.tv)
                   v1.text=data.t1 
                }
            }
            .addTypeBinder<Data.Type2>(R.layout.adapter_item2){
                onBindView { holder, view, pos, data, payloads -> 
                    //type 2 bind view
                    val v2= holder.find<TextView>(R.id.tv2)
                    v2.text=data.t2
                }
            }                    

#State 提供三種狀態error、empty、loading顯示的畫面

GenericAdapter(list)
   // ...
   .emptyBinder(R.layout.adapter_empty)//當dataList size為0時 會自動顯示emptyView
   
   .errorBinder(R.layout.adapter_error){//當要顯示errorView時需手動setState(GenericAdapter.State.ERROR)
      onBindView { holder, v ->
                    val btn:Button = holder.find(R.id.btn_retry)
                    btn.setOnClickListener {
                        adapter.submitList(getData())
                    }
      }
    }
   .loadingBinder(R.layout.adapter_loading)//當要顯示loadingView時需手動setState(GenericAdapter.State.LOADING)

LoadMore

adapter.getLoadMore().setListener(
            object :LoadMore.Callback{
                override fun more() {
                  //load more
                  adapter.getLoadMore().lock()
                  //get more data 
                
                }
            }
        )

何時載入更多?

adapter.getLoadMore().prefetchDistance(5)//還剩多少的item時載入更多,預設為0(到最底才載入)

載入前,呼叫lock(),防止多次上滑載入

adapter.getLoadMore().lock()

載入完,呼叫unlock(),如果已經完全載入完成則不必呼叫。

adapter.getLoadMore().unlock()

Submit

呼叫adapter.submitList()更新data,內部實現DiffUtil計算出兩個列表間差異,自動實現非全量更新。

adapter.submitList(getData())

當然,你也可以使用原生的更新方式

notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
// etc...
You might also like...
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView
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

A flexible view for providing a limited rect window into a large data set,just like a two-dimensional RecyclerView.  It different from RecyclerView is that it's two-dimensional(just like a Panel) and it pin the itemView of first row and first column in their original location. 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

A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView
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

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

TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.
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

[] Super fast and easy way to create header for Android RecyclerView

DEPRECATED I created this library back in the day when I thought RecyclerView was all new and difficult. Writing an adapter that could inflate multipl

A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.
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

Releases(1.2.1)
Owner
JaredDoge
JaredDoge
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
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
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Nov 21, 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
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
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
Add RecyclerView, use Adapter class and ViewHolder to display data.

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

Shaima Alghamdi 3 Nov 18, 2021