Closed deakjahn closed 9 years ago
Usage, although it's quite obvious, in the adapter extended from the above:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_SECTION) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listheader, parent, false);
return new HeaderViewHolder(itemView);
}
else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
return new ItemViewHolder(itemView);
}
}
and
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, Cursor cursor) {
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder view = (HeaderViewHolder) holder;
...
}
else {
ItemViewHolder view = (ItemViewHolder) holder;
...
}
}
Internally we don't name with m ourselves. I do it for open source to be compatible with Android guidelines.
Other comments: The changeCursor method concerns me for two reasons; 1. it bypasses the swapCursor method which all of the data is build on, I see a lot of index and null errors occuring from this, and 2. you should never close a cursor in an adapter because that adapter does not own that cursor.
I would like to see a getCursor method for general usage.
I would also like to see a createSectionViewHolder()
, bindSectionViewHolder()
, createItemViewHolder()
and bindItemViewHolder()
. It would remove that simple boiler plate logic out of the implementing class.
Looks good. I would take this as a pull request with the above changes.
Yes, you caught me. I actually use this class implementing a CursorLoader itself, so it does own the cursor all right (and if it doesn't close them, strict mode results in an exception, of course). When I edited the loader out for more general use, those closes might have remained by mistake.
As to the rest, I dunno. You did it that way in the original version, yes, but the usual RW way seems to be this one, sending the view type up to the using class. In my actual case, this happens to be better because I reuse this same class over and over again to display different sectioned lists...
SectionCursorAdapter 3.0 now has experimental RecyclerViewAdapters.
For your pleasure and delectation, this is a version I use successfully with a RW:
Sorry for the variable name changes, no matter how conventional it is, I just can't force myself to use mNames... :-)