WonderAdapter
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.
Usage
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:
- Create Custom
Holder
and let implementSingleWonder, 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. - Basic methods to implement (on single views): ..*
W newInstance()
returns an instance ofW
(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 classW
. ..*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());
listView.setAdapter(adapter);
and our SingleViewHolder implementation:
public class SingleViewHolder implements SingleWonder<Wonder, SingleViewHolder> {
// UI
@InjectView(R.id.row_wonder_image) ImageView imageView;
@InjectView(R.id.row_wonder_title) TextView titleView;
@Override public SingleViewHolder newInstance() {
return new SingleViewHolder();
}
@Override public void bind(Context context, Wonder item) {
Picasso.with(context).load(item.getImage()).into(imageView);
titleView.setText(item.getTitle());
}
@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