Monday, 16 February 2015

Wonder Adapter


How many times do you copy ArrayAdapter, CursorAdapter and ViewHolders code? I've developed this library based mostly on the idea of UniversalAdapter library, but I've implemented a few improvements like ViewHolder pattern, multi view and cursor compatibility.


You just have to use the adapter that fits better to your needs. For now, there are only 3 wonder adapters you can use:
  • WArrayAdapter
  • WBaseAdapter
  • WCursorAdapter
Steps to use any of these adapters within a ListView:
  1. Create Custom Holder and let implement SingleWonder, MultiWonder or CursorWonder depending on the Adapter we want to use with. ..* T is the class of the item we want to show. ..* W the Holder class.
  2. Basic methods to implement (on single views): ..* W newInstance() returns an instance of W(holder) for every row in the list. Our holder contains row view fields initialized. ..*void bind(...) needed to draw desired object fields on the initialized view fields contained inside our class W. ..* View inflateView(...) needs explanation? :-).
Example: ListView with array list of items within a single row view
WArrayAdapter<Wonder, SingleViewHolder> adapter = new WArrayAdapter(this, getData(cursor), new SingleViewHolder());
and our SingleViewHolder implementation:
public class SingleViewHolder implements SingleWonder<Wonder, SingleViewHolder> {

  // UI
  @InjectView( ImageView imageView;
  @InjectView( TextView titleView;

  @Override public SingleViewHolder newInstance() {
    return new SingleViewHolder();

  @Override public void bind(Context context, Wonder item) {

  @Override public View inflateView(LayoutInflater inflater, ViewGroup parent) {
    View view = inflater.inflate(R.layout.row_wonder, parent, false);
    ButterKnife.inject(this, view);
    return view;

I've used Jake Wharton's Butterknife library to avoid using findViewById on every view in our row layout :-)
Check the code for full demo samples.

No comments:

Post a Comment