A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

Overview

##PowerfulRecyclerViewAdapter Bintray android-arsenal Apache 2

A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as remove,add,etc.

###Features

  • Operate on data set,for example ,remove,add,get,etc.
  • Bind Data(Model) and ViewHolder using DataBean,DataBean is a Wrapper of Data(Model);
  • DataBean controls creation of the ViewHolder and binds data to the ViewHolder;
  • Separate part of Adapter's responsibilities to DataBean,such as creating an instance of the ViewHolder and binding data to ViewHolder.Adapter only operate data;
  • Remove all switch..case statemens in onCreateViewHolder() or onBindViewHolder() taking advantage of Polymorphism;
  • You don't need to write any Recycler.Adapters after you use this powerful common adapter;
  • Your RecyclerView can have any kind of items(or viewHolders).

###New features

Added on 4/28/2016(dev branch):
1.add setListener() method in CommonRecyclerAdapter.Extend CommonRecyclerAdapter and override setListener() if you want set holder'listener outside of the ViewHolder,If not,you can use CommonRecyclerAdapter as your RecyclerView's adapter directly and set Listeners in each kind of ViewHolder

 Added on 4/25/2016:
1.add HolderAPI and HolderHelper which can be reused in all kinds of ViewHolders,even ListView,GridViews.

 Added on 4/16/2016:
1.add some useful apis in BaseRecyclerViewHolder,such as setText(id,text),setImageBitmap(id,bitmap),etc to simplify your ViewHolder coding;
2.add some useful and friendly apis in BaseRecyclerAdapter,such as removeData(data),removeFirst(),removeLast(),etc;
3.use a SparseArray to cache views in the ViewHolder,see BaseRecyclerViewHolder for detail.

Added on 4/10/2016:
1.@DataBean Annotation
Use apt(Annotation Processor Tool) like used in Dagger2 and DataBinding to process annotations and generate DataBean source code for you,you don't need to write databean classes anymore,that's a progress.
see [Use @DataBean] guide module

###Important classes:

class CommonRecyclerAdapter:common adapter whose super class is RecyclerView.Adapter,supports inserting and droping datas and supports any kind of items.

interface DisplayBean:used to create an instance of the ViewHolder

interface DataBean:extends from DisplayBean,used to bind data to viewholders

###How To:

Setup:

  • add apt classpath dependencies to your project's gradle file:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  • apply apt plugin to your app's gradle flie and must apply after application plugin:
apply plugin: 'com.neenbedankt.android-apt'
  • add PowerfulRecyclerViewAdapter dependencies to your app's gradle file:
compile 'com.creact:powerful-adapter-lib-release:1.0.0@aar'
provided 'com.creact:powerful-adapter-annotations-release:1.0.0'
apt 'com.creact:powerful-adapter-complier-release:1.0.0'//apt

Use:

  • Declare CommonRecyclerAdapter as your RecyclerView's adapter;
 protected CommonRecyclerAdapter adapter;
 ...
 recyclerView.setAdapter(adapter);
  • Extend BaseDataBean,create your DataBean;
public class BookTitleBean extends BaseDataBean<Book, BookTitleViewHolder> {

    public BookTitleBean(Book data) {
        super(data);
    }

    @Override
    public BookTitleViewHolder createHolder(ViewGroup parent) {
	    //create an instance of Your ViewHolder
        return new BookTitleViewHolder(getView(parent, BookTitleViewHolder.LAYOUT_ID));
    }
}
  • Extend BaseRecyclerViewHolder,create your ViewHolder;
public class BookTitleViewHolder extends BaseRecyclerViewHolder<Book> {
	//declare LAYOUT_ID
    public static final int LAYOUT_ID = R.layout.item_book_title;

    public BookTitleViewHolder(View itemView) {
        super(itemView);
    }

    @Override
    public void setData(Book data) {
        if (data == null)
            return;
        setText(R.id.name, data.getName());
        setText(R.id.price, String.valueOf(data.getPrice()));
    }
}
  • Construct DisplayBeans:convert data set to displaybean set
/**
     * Convert normal DataSet to DisplayBeans
     * If the data set which  returned by the server in the same order as in the list on ui,
     * this process will be easy
     */
    protected void initData() {
        //fake data
        Map<ICategory, List<Book>> sourceData = fetchSourceData();
        //display beans
        List<DisplayBean> displayBeans = new ArrayList<>(20);

        //Add a ProgressBar DisplayBean to show a ProgressBar in the top of the list
        CommonDisplayBean progressBean = new CommonDisplayBean(R.layout.item_progress);
        displayBeans.add(progressBean);

        //add categories and books to the list
        for (Iterator<Map.Entry<ICategory, List<Book>>> iterator = sourceData.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<ICategory, List<Book>> entry = iterator.next();
            ICategory category = entry.getKey();
            //add category to the list
            displayBeans.add(new CategoryBean(category));
            List<Book> books = entry.getValue();
            //add books to the category
            if (category != null && books != null) {
                for (Book book : books) {
                    BookTitleBean bookTitleBean = new BookTitleBean(book);
                    displayBeans.add(bookTitleBean);
                }
            }
        }
        //load data and show data in the list
        adapter.loadData(displayBeans);
    }

Screenshot:

screenshot1

  • For items have no data or only display static data,for example,just show a progressBar,use CommonDisplayBean which creates an instance of BaseRecyclerViewHolder

screenshot2

  • Reuse ViewHolder and DataBean

Use interface or abstract class (type parameter of BaseRecyclerViewHolder)to bind DateBean and ViewHolder

Demo: ####ICategory interface

/**
* An interface represents an abstract category
*/
public interface ICategory {

   String getName();
   long getId();
}

####CategoryBean:

public class CategoryBean extends BaseDataBean<ICategory, CategoryViewHolder> {

   public CategoryBean(ICategory data) {
       super(data);
   }

   @Override
   public CategoryViewHolder createHolder(ViewGroup parent) {
       return new CategoryViewHolder(getView(parent, CategoryViewHolder.LAYOUT_ID));
   }
}

####CategoryViewHolder

public class CategoryViewHolder extends BaseRecyclerViewHolder<ICategory> {
    public static final int LAYOUT_ID = R.layout.item_book_catagory;
    public CategoryViewHolder(View itemView) {
        super(itemView);
    }
    @Override
    public void setData(ICategory category) {
        if (category == null)
            return;
        setText(R.id.book_category,category.getName());
    }

}

Use @DataBean

Because BaseDataBean and its super classes have done much work for us,our DataBean's code is very simple,only have couple of lines.Simpler than Simpler,we use apt to genreate your DataBean source code.The tool is holder-compiler.Now you can use @DataBean on your ViewHolder,The apt will generate corresponding source code for you as you wanted,it's very convenient.

How to:take BookTitleViewHolder for example

//use DataBean annotation to annotate your ViewHolder
@DataBean(beanName = "BookTitleBean", data = Book.class)
public class BookTitleViewHolder extends BaseRecyclerViewHolder<Book> {

    public static final int LAYOUT_ID = R.layout.item_book_title;

    public BookTitleViewHolder(View itemView) {
        super(itemView);
    }

    @Override
    public void setData(Book data) {
        if (data == null)
            return;
        setText(R.id.name, data.getName());
        setText(R.id.price, String.valueOf(data.getPrice()));
    }
}

1.Extend BaseRecyclerViewHolder to create your ViewHolder;

Notes:public field LAYOUT_ID of your ViewHolder is 
required ,and the name must be LAYOUT_ID(apt need this constant).

2.Use @DataBean on your ViewHolder(only for classes whose super class is BaseRecyclerViewHolder)

Elements of DataBean:

  • beanName->simple class name of the generated DataBean,String type;
  • data->type of data which wrapped by DataBean and used by ViewHolder,Class type.

3.build the project,apt will generated the DataBean code for you,and generated BookTitleBean like this: app\build\generated\source\apt\debug\ [package]\BookTitleBean.java

package com.steve.creact.powerfuladapter.presentation.viewholder.databean;

import android.view.ViewGroup;

import com.steve.creact.library.display.BaseDataBean;
import com.steve.creact.powerfuladapter.data.Book;
import com.steve.creact.powerfuladapter.presentation.viewholder.BookTitleViewHolder;

/**
 * Generated DataBean for BookTitleViewHolder
 * Powered by Holder-Compiler
 */

public class BookTitleBean extends BaseDataBean<Book, BookTitleViewHolder> {

    public BookTitleBean(Book data) {

        super(data);
    }

    @Override
    public BookTitleViewHolder createHolder(ViewGroup parent) {
        return new BookTitleViewHolder(getView(parent, BookTitleViewHolder.LAYOUT_ID));//that's why need LAYOUT_ID field in ViewHolder
    }
}

Just like handwriting code!

One More Tip:

Use File Template function of IDE to create a common ViewHolder class template! Right click the package you want to put ViewHolders in, select "New" menu,then select "Edit File Template",add your custom ViewHolder class file template,my template is like this:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
@DataBean( beanName = "${DATA}DataBean",data = ${DATA}.class)
public class ${DATA}ViewHolder extends BaseRecyclerViewHolder<${DATA}> {

  public static final int LAYOUT_ID = R.layout.your_layout_id;

  public ${DATA}ViewHolder( View itemView ) {
    super( itemView );
  }

  @Override
  public void setData( ${DATA} data ) {
    if ( data == null ) return;
  }
}

${DATA} is a placeholder which represents class name of your data. After creating a ViewHolder file template,every time you want write a ViewHolder, you can use the template to create it then modify the variable part.

problems may occur when you build:

  • can't delete holder-compiler.jar->open task manager,end process of java se,rebuild.

###Contact Me:

Email:[email protected]

Weibo:http://weibo.com/u/3398987850

Github:https://github.com/simplify20

CSDN:http://blog.csdn.net/u012825445

You might also like...
Reproducible sample with Fix for Memory Leak in RecyclerView Adapter
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

RecyclerView With No Adapter | Available For Jetpack Compose
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

Easy RecyclerView Adapter

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

Just another one easy-to-use adapter for RecyclerView :rocket:
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

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

Add error handling in RecyclerView
Add error handling in RecyclerView

MarsPhotosAPi Exercises Components Used Recycler View API enu The Second Image when i Turn off wifi . References https://developer.android.com/courses

A layout manager for the RecyclerView with interchangeable linear, grid, and staggered displays of views, all with configurable section headers including the sticky variety as specified in the material design docs.

SuperSLiM This is the version 5 development branch. Project Plan Support me on Patreon Blog What is Version 5 Version 5 is the current development bra

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

Comments
  • AS2.1.2无法进行编译了

    AS2.1.2无法进行编译了

    截止到7月26日,AS和SDK都是最新的 下午的时候突然无法编译,提示自动生成的所有Bean找不到,不存在 我试过了clean,invalidate caches and restart,重启AS,重启电脑,删除build目录,删除gradle中的PowerfulAapter的配置,然后重新添加依赖,依然都是不行的。。

    opened by hidetag 2
Releases(v1.0.0)
  • v1.0.0(Apr 25, 2016)

    • Operate on data set,for example ,remove,add,get,etc.
    • Bind Data(Model) and ViewHolder using DataBean,DataBean is a Wrapper of Data(Model);
    • DataBean controls creation of the ViewHolder and binds data to the ViewHolder;
    • Separate part of Adapter's responsibilities to DataBean,such as creating an instance of the ViewHolder and binding data to ViewHolder.Adapter only operate data;
    • Remove all switch..case statemens in onCreateViewHolder() or onBindViewHolder() taking advantage of Polymorphism;
    • You don't need to write any Recycler.Adapters after you use this powerful common adapter;
    • Your RecyclerView can have any kind of items(or viewHolders);
    • @DataBean Annotation
    • Useful apis in BaseRecyclerViewHolder,such as setText(id,text),setImageBitmap(id,bitmap),etc
    Source code(tar.gz)
    Source code(zip)
Owner
null
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
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
Android library for RecyclerView to manage order of items and multiple view types.

recyclerview-binder Android Library for RecyclerView to manage order of items and multiple view types. Features Insert any items to wherever you want

Satoru Fujiwara 185 Nov 15, 2022
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
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
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
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
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