Skip to content

A MultiType Recyclerview Adapter using Data Binding | 使用databinding实现适应多种type的Recyclerview adapter

License

Notifications You must be signed in to change notification settings

BoBoMEe/DataBindingDemo

Repository files navigation

DataBindingDemos


中文版 README

DataBinding RecyclerViewAdapter

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.

It`s Based on markzhai/DataBindingAdapter and tianzhijiexian/CommonAdapter

Combined with the Characteristics of the two, make Adapter easier to use

Get Started

AdapterItem:

public class EmployerItem implements AdapterItem<EmployerViewModel> {
//...
  @Override public int getLayoutResId() {
    return R.layout.item_employer;// layout res
  }

  @Override public Integer getVariableId() {
    return BR.item; // xml中的item
  }

  @Override public Decorator getDecorator() {
    return null; // item Decorator
  }

  @Override public Map<Integer, Object> getBindData() {
    Map<Integer, Object> map = new HashMap<>();
    map.put(BR.presenter, new EmployerPresenter());
    return map; // item <-> Variable
  }

  public class EmployerPresenter {//presenter
    public void onItemClick(EmployerViewModel model) {
      Toast.makeText(context, "employer " + model.name, Toast.LENGTH_SHORT).show();
    }
  }
}

CommonAdapter:

public class MainAdapter extends CommonAdapter {
  //...
  @NonNull @Override public AdapterItem createItem(int viewType) {
    return adapterItem;
  }

  @Override public int getViewType(Object _o) {
    return viewType;
  }
}

Listener Binding

An easy way to add listener binding

 holder.getBinding().setVariable(key, value);

Do not have their own settings, only need to set up presenter,Such as:

 public class EmployerPresenter {//presenter
    public void onItemClick(EmployerViewModel model) {
      Toast.makeText(context, "employer " + model.name, Toast.LENGTH_SHORT).show();
    }
  }

Decorator

A Decorator is provided to let user implement and set ,With it we can do some extra works in onBindViewHolder

AdapterItem.Decorator decorator = adapterItem.getDecorator();

if (decorator != null) {
      decorator.decorator(holder, position, getItemViewType(position));
}

//adapter
@Override public Decorator getDecorator() {
    return null; // return AdapterItem.Decorator
  }

Thanks

Links

License

Copyright (C) 2016 BoBoMEe (wbwjx115@gmail.com).

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.

About

A MultiType Recyclerview Adapter using Data Binding | 使用databinding实现适应多种type的Recyclerview adapter

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages