- A common ListView / RecyclerView adapter.
- Support RecyclerView multiple view type. This is a demo
allprojects {
repositories {
// ...
maven { url "https://jitpack.io" }
}
}
implementation "com.github.twiceyuan.CommonAdapter:library:$latest_version"
implementation "com.github.twiceyuan.CommonAdapter:kotlin-extension:$latest_version"
- 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);
}
}
-
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)); });
-keepattributes *Annotation*
-keepclassmembers class * extends com.twiceyuan.commonadapter.library.holder.CommonHolder {
public <init>(...);
}
- EasyAdapter https://github.com/ribot/easy-adapter
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.