• Stars
    star
    245
  • Rank 165,304 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Super simple RecyclerView adapter using Data Binding Technology.

DataBinding RecyclerViewAdapter

中文版 README

Free from writing adapters! NO MORE CLASSES!

Super simple RecyclerView adapter using Data Binding Technology, no longer need to write any adapter! You don't need to write any extra class like ViewHolder or ItemView.

One more reason you should try Data Binding!

Get Started

dependencies {
    compile 'com.github.markzhai:databinding-rv-adapter:1.0.1'
}

This library provide two types of RecyclerView.Adapter.

SingleTypeAdapter:

SingleTypeAdapter<EmployeeViewModel> adapter = new SingleTypeAdapter<>(this, R.layout.item_single_type);

MultiTypeAdapter:

MultiTypeAdapter adapter = new MultiTypeAdapter(this);
adapter.addViewTypeToLayoutMap(VIEW_TYPE_HEADER, R.layout.item_header);
adapter.addViewTypeToLayoutMap(VIEW_TYPE_CODER, R.layout.item_coder);
adapter.addViewTypeToLayoutMap(VIEW_TYPE_BOSS, R.layout.item_boss);

The only limitation is naming convention: your view model in xml should be named item, and your presenter should be named presenter, which I do think is a good practice.

Advanced Usage

Manipulation

singleTypeAdapter.addAll(EMPLOYEE_LIST);
// clear and addAll
singleTypeAdapter.set(EMPLOYEE_LIST);
singleTypeAdapter.add(employee);
// add with specific position
singleTypeAdapter.add(2, employee);

multiTypeAdapter.add(null, VIEW_TYPE_HEADER);
multiTypeAdapter.addAll(EMPLOYEE_LIST, VIEW_TYPE_EMPLOYEE);
multiTypeAdapter.addAll(EMPLOYER_LIST, VIEW_TYPE_EMPLOYER);
multiTypeAdapter.add(2, employee, VIEW_TYPE_EMPLOYEE);

// Like clear + addAll
multiTypeAdapter.set(EMPLOYEE_LIST, VIEW_TYPE_EMPLOYEE);

// both have these methods
adapter.clear();
adapter.remove(2);

Listener Binding

The library also provide an easy way to add listener binding, and by default provide a whole item click listener.

public class DemoAdapterPresenter implements BaseViewAdapter.Presenter {
    public void onItemClick(EmployeeViewModel model) {
        Toast.makeText(MainActivity.this, "employee " + model.name, Toast.LENGTH_SHORT).show();

    }
    public void onItemClick(EmployerViewModel model) {
        Toast.makeText(MainActivity.this, "employer " + model.name, Toast.LENGTH_SHORT).show();
    }
}

multiTypeAdapter.setPresenter(new DemoAdapterPresenter());

singleTypeAdapter.setPresenter(new SingleTypeAdapter.Presenter<EmployeeViewModel>() {

    @Override
    public void onItemClick(EmployeeViewModel model) {
        Toast.makeText(MainActivity.this, model.name, Toast.LENGTH_SHORT).show();
    }
});

Decorator

Sometimes, we may want to do some extra works in onBindViewHolder, thus we provide a Decorator to let user implement and set it in.

public class DemoAdapterDecorator implements BaseViewAdapter.Decorator {

    @Override
    public void decorator(BindingViewHolder holder, int position, int viewType) {
        // you may do something according to position or view type
    }
}

adapter.setDecorator(new DemoAdapterDecorator());

Contributors

Welcome for issues and PR to fulfill your own features.

You may have interest in

They are somehow likely and powerful, enable you to save codes. However, both of them have the problems that lose flexibility and force you to create something like a wrapper which is annoyed.

License

Copyright (C) 2016 MarkZhai (http://zhaiyifan.cn).

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.

More Repositories

1

AndroidPerformanceMonitor

A transparent ui-block detection library for Android. (known as BlockCanary)
Java
6,572
star
2

LyricHere

Local lyric-play support music player, includes a powerful LyricView
Java
329
star
3

init

Init helps app schedule complex task-group like initialization with type, priority and multi-process.
Java
272
star
4

LyricView

Android LyricView that accepts lrc stream as input, A powerful lyric parser is also contained.
Java
264
star
5

RememberEditText

An EditText which can remember last several input, free developer from managing cache everywhere themselves. !! NOT ViewGroup !!
Java
163
star
6

DataBindingSample

Android Data Binding Sample
Java
156
star
7

InterestingTitleBar

TitleBar (or custom ActionBar) with immersive status bar and scroll animation like tumblr app (icon and text color gradually change to end color).
Java
111
star
8

react-native-preloader

React Native Pre-loader for Android.
Java
53
star
9

ResourceOverlayDemo

Demo to show Android aapt overlay and gradle resource merge
Java
23
star
10

AndroidProjectFrom0

Repository to show the best practice from the series blog Android-Project-From-0
Java
20
star
11

GoWebServer

A web server use Go language, includes authentication.
Go
10
star
12

markzhai.github.io

Mark Zhai's hexo homepage
HTML
10
star
13

nevo-direct-reply

Decorators for nevolution which provides direct reply experience on wechat, whatsapp, etc.
Java
9
star
14

GitHubExplorer

(wip) An github android client with explore and trending feature.
Java
8
star
15

ErogameScape-Mining

A spider and data-mining project on ErogameScape
Python
5
star
16

PlaneWar

A clone of initial version of 飞机大战 from 微信. SurfaceView implementation
Java
4
star
17

add-fitsSystemWindows

add fitsSystemWindows to all layout files (use if to add Translucent Status Bar support easily)
Python
3
star
18

Logger

Logger is a logging tool helps unify release log and debug log, with some little advanced usage
Java
3
star
19

HelloVue

Repository for Android developer to learn Vue.js
JavaScript
3
star
20

Galaxy

Galaxy is a hybrid framework for Android.
3
star
21

dimBody

Screen dimming according to the barrier in front of the screen for CSIT6910 HKUST.
C++
1
star
22

blog-comment

blog comment
1
star
23

MusicPlayer

A Music player implemented by Java and Processing, it has wonderful music visualization and rhythm game for any music.
Java
1
star