百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

multi-type-adapter

> 编程语言
开源

基于 Android 数据绑定的超级简单、易于使用的通用多类型适配器

100 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

基于 Android 数据绑定的超级简单、易于使用的通用多类型适配器

MultiTypeAdapter

Implement a super simple, powerful, and easy to use MultiTypeAdapter for RecyclerView by android databinding, it is only about 100 lines code and just one java file, use this adapter, you can never to implement kinds of ViewHolder anymore.

I write a very detail tutorial to explain how to implement this MultiTypeAdapter, and how to use it to implement a complete sample that support refresh, load more, retry, header item, emtpy item, error item, footer item, any kinds of data item step by step.

Tutorial

  1. Escape the nightmare of adapter and viewholder by android databinding (1)
  2. Escape the nightmare of adapter and viewholder by android databinding (2)

Sample

  1. Demo HeaderItem, EmptyItem, ErrorItem, refresh, load more, load error and retry:

  2. Demo load more but has no more data:

  3. Polish the UI, add event handler for item:

The sample includes following features, I think it should fullfill 90% needs:

  1. Refresh
  2. Load more
  3. Support header item
  4. Support empty item and enable refresh again
  5. Support error item and enable refresh again
  6. Support footer item, includes 3 states: loading, load error and enable retry, no more data
  7. Support any kind of data item, here just demo ImageItem and TextItem

Download APK | GitHub

There are two folders in this project, MultiTypeAdapterSample and MultiTypeAdapterTutorial, their codes are nearly same, the latter is created for the above tutorial articles, I tag the every key step so you can easily compare the code with article.

Getting Started

Add JitPack as library source in your project build.gradle:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Then, add dependency in your app module build.gradle:

dependencies {
    compile 'com.github.baurine:multi-type-adapter:${latest-version}'
}

latest-version: see top jitpack badge.

And enable databinding in your app module build.gradle:

android {
    ...
    dataBinding {
        enabled = true
    }
}

Usage

Please read the above super detail tutorial to learn how exactly to use it. Following are some simple instruction extract from tutorial.

After you use this MultiTypeAdapter, you just need to implement kinds of items, and add to or remove from adapter at the right time. The item represents a whole body that display in recyclerview, includes layout, data and event, so it is a wrapper, wrap the layout, data model and event handler together, but the data model and event handler are not necessary, just layout is required, it depends on what's the kind of item. All items must inherit from IItem interface:

public interface IItem {
    // get the xml layout this type item used in
    int getLayout();

    // get the variable name in the xml
    int getVariableId();
}

getLayout() method should return the xml layout, likes R.layout.item_header, and getVariableId() return the variable name this item used in xml, likes BR.item. Becasuse we usually use a same variable name in all items, and the event handler, usually it can be a View.OnClickListener, so we define a BaseItem:

public abstract class BaseItem implements MultiTypeAdapter.IItem {
    @Override
    public int getVariableId() {
        return BR.item;
    }

    ////////////////////////////////////////////
    // handle event
    private View.OnClickListener onClickListener;

    public View.OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}

Here is an example, a most simple item - HeaderItem, just has layout, no data and no event:

public class HeaderItem extends BaseItem {
    @Override
    public int getLayout() {
        return R.layout.item_header;
    }
}

And HeaderItem used in item_header.xml:

A most complex item - ImageItem, display a random image, and you can like it, hide it, and comment it, the image data from ImageModel:

ImageModel:

public class ImageModel extends BaseModel {
    @Override
    public MultiTypeAdapter.IItem createItem(MultiTypeAdapter adapter) {
        return new ImageItem(this, adapter);
    }

    ////////////////////////////////////////
    public final String url;
    public boolean liked;

    public ImageModel() {
        super();
        url = "https://unsplash.it/200/200?random&" + new Random().nextInt(40);
        liked = (new Random()).nextBoolean();
    }
}

ImageItem:

public class ImageItem extends BaseItem {
    @Override
    public int getLayout() {
        return R.layout.item_image;
    }

    ///////////////////////////////////////
    public ImageItem(ImageModel imageModel, final MultiTypeAdapter adapter) {
        this.imageModel = imageModel;

        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv_like:
                        toggleLiked();
                        adapter.notifyItemChanged(adapter.findPos(ImageItem.this));
                        break;
                    case R.id.tv_hide:
                        adapter.notifyItemRemoved(adapter.removeItem(ImageItem.this));
                        break;
                    case R.id.tv_comment:
                        CommonUtil.showToast(view.getContext(),
                                "TODO: comment image, id: " +
                                        String.valueOf(getId()));
                        break;
                }
            }
        });
    }

    ///////////////////////////////////////
    // data model part
    private final ImageModel imageModel;

    public String getUrl() {
        return imageModel.url;
    }

    public boolean isLiked() {
        return imageModel.liked;
    }

    public int getId() {
        return imageModel.id;
    }

    public void toggleLiked() {
        imageModel.liked = !imageModel.liked;
    }
}

ImageItem used in item_image.xml:

Then call adapter.addItem(), adapter.removeItem(), adapter.notifyItemChanged(adapter.findPos(item)) to operate the adapter at the right time. Following is an example to handle the network response result:

private void retrieveItems(boolean loadMore) {
    // result = 0, network error
    // result = 1, empty
    // result = 2, last page data
    // result = 3 and other, normal result
    int resultType = (new Random()).nextInt(100) % 5;
    if (resultType == 0) {
        adapter.addItem(loadMore ? footerItem.setState(FooterItem.ERROR) : errorItem);
    } else if (resultType == 1) {
        adapter.addItem(loadMore ? footerItem.setState(FooterItem.NO_MORE) : emptyItem);
    } else if (resultType == 2) {
        addDataItems(PER_PAGE_COUNT / 2);
        // here depends whether you want to display no more data state
        // if you don't want to display this state when has no more data
        // then just don't add it back
        adapter.addItem(footerItem.setState(FooterItem.NO_MORE));
    } else {
        addDataItems(PER_PAGE_COUNT);
        // pre-display loading state to improve user experience
        adapter.addItem(footerItem.setState(FooterItem.LOADING));
    }
}

License

Copyright 2017 baurine.

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaandroid-databindingmulti-type-adapterrecyclerview

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言