A library for for simplifying adapter creation, support List, RecyclerView, ViewPager.
ViewHolder
public class DataGetter {
final public T previous;
final public T next;
final public T data;
public DataGetter(T previous, T data, T next) {
this.previous = previous;
this.next = next;
this.data = data;
}
}
public interface IViewHolder {
// create view and bind to this view holder
void bind(View view);
// position: item position in list
// getter: access current item and previous and next, this is useful when decide show or hide some view depened on previous item or next item or all
void onDataChange(DataGetter getter, int position);
}
Copy the code
ViewCreator
public interface IViewCreator {
// call in getView
View view(ViewGroup container);
// call in getItemViewType
int viewTypeFor(T data, int position, int itemCount);
// call in getViewTypeCount
int viewTypeCount();
}
Copy the code
Single Layout
// ViewCreator just a implement of IViewCreator, accept layout id and view holder factory
new AutoListAdapter(items, new ViewCreator(R.layout.list_item, () -> new ViewHolder());
Copy the code
Multiple Layout
// ViewCreatorCollection just another implement of IViewCreator, it create different view depends on values of item and position and itemCount.
// It useful when getViewTypeCount > 1
ViewCreatorCollection collection = new ViewCreatorCollection.Builder()
.addFilter(data, position, itemCount) -> position == 1, R.layout.list_item_1, ViewHolder1::new)
.addFilter((data, position, itemCount) -> position == 2, R.layout.list_item_2, ViewHolder2::new).build();
new AutoListAdapter(items, collection);
Copy the code
Paging ListView
ViewCreatorCollection collection = new ViewCreatorCollection.Builder()
.loadingResId(R.layout.list_item_loading)
.addFilter(R.layout.list_item, ViewHolder::new)
.build();
// pagingListener will be called when arrive the last position in ListView
new AutoListPagingAdapter(stocks, collection, pagingListener);
Copy the code
Using with Gradle
Dependencies {the compile 'com. Benny. Library: autoadapter: 0.2.8'}Copy the code
Discussion
QQ Group: 516157585