/CommonAdapter

通用 RecyclerView 适配器,用最少的代码使用 Adapter

Primary LanguageJava

CommonAdapter

Build Status Jitpack Apache 2.0

中文介绍

  • A common ListView / RecyclerView adapter.
  • Support RecyclerView multiple view type. This is a demo

Intro

Sample Download

Get it on Google Play

Setup

allprojects {
  repositories {
    // ...
    maven { url "https://jitpack.io" }
  }
}
implementation "com.github.twiceyuan.CommonAdapter:library:$latest_version"
implementation "com.github.twiceyuan.CommonAdapter:kotlin-extension:$latest_version"

(ext.latest_version= Jitpack)

Usage

  1. create a ViewHolder extends CommonHolder<T>,T is a model class
// bind layout id
@LayoutId(R.layout.item_person)
public class PersonHolder extends CommonHolder<Person> {

    // bind View id
    @ViewId(R.id.name) TextView name;
    @ViewId(R.id.email) TextView email;

    // bind data
    @Override public void bindData(Person person) {
        name.setText(person.name);
        email.setText(person.email);
    }
}
  1. Create a common adapter and call setAdapter method of list view or recycler view.

    RecyclerView Adapter

    // build adapter
    CommonAdapter<Person, PersonHolder> recyclerAdapter = new CommonAdapter<>(this, PersonHolder.class);
    
    // set adapter
    mRecyclerView.setAdapter(recyclerAdapter);

    ListView Adapter

    // build adapter
    CommonListAdapter<Person, PersonHolder> listAdapter = new CommonListAdapter<>(this, PersonHolder.class);
    mListView.setAdapter(listAdapter);

    Setup listener (by holder)

    recyclerAdapter.setOnBindListener((position, person, holder) -> {
        // holder is used to bind event listener
        holder.name.setOnClickListener((v) -> toast(person.name));
        holder.email.setOnClickListener((v) -> toast(person.email));
    });

Proguard

-keepattributes *Annotation*
-keepclassmembers class * extends com.twiceyuan.commonadapter.library.holder.CommonHolder {
    public <init>(...);
}

Thanks

License

Copyright 2016 twiceYuan.

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.