DataBinding RecyclerView Adapter

DataBinding RecyclerView Adapter for Android

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.markzhai
ArtifactId

ArtifactId

databinding-rv-adapter
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

aar
Description

Description

DataBinding RecyclerView Adapter
DataBinding RecyclerView Adapter for Android
Project URL

Project URL

https://github.com/markzhai/DataBindingAdapter
Source Code Management

Source Code Management

https://github.com/markzhai/DataBindingAdapter

Download databinding-rv-adapter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.markzhai/databinding-rv-adapter/ -->
<dependency>
    <groupId>com.github.markzhai</groupId>
    <artifactId>databinding-rv-adapter</artifactId>
    <version>1.0.1</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.markzhai/databinding-rv-adapter/
implementation 'com.github.markzhai:databinding-rv-adapter:1.0.1'
// https://jarcasting.com/artifacts/com.github.markzhai/databinding-rv-adapter/
implementation ("com.github.markzhai:databinding-rv-adapter:1.0.1")
'com.github.markzhai:databinding-rv-adapter:aar:1.0.1'
<dependency org="com.github.markzhai" name="databinding-rv-adapter" rev="1.0.1">
  <artifact name="databinding-rv-adapter" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.markzhai', module='databinding-rv-adapter', version='1.0.1')
)
libraryDependencies += "com.github.markzhai" % "databinding-rv-adapter" % "1.0.1"
[com.github.markzhai/databinding-rv-adapter "1.0.1"]

Dependencies

compile (4)

Group / Artifact Type Version
com.android.support » recyclerview-v7 jar 24.2.0
com.android.databinding » library jar 1.1
com.android.databinding : baseLibrary jar 2.1.3
com.android.databinding » adapters jar 1.1

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.0.1
1.0.0