Hello. I'm writing this kind of thing for the first time, so I don't know what to do, but thank you.
I used ListView on my Android app ↓ I want to use Coordinator Layout ↓ Let's use RecyclerView ↓ The header and footer cannot be attached quickly ↓ I made an Adapter so that I can attach a header and footer ↑ I will introduce the Adapter that I made at this time. (I put it on GitHub.) I hope it helps someone. (I wonder if it already exists) I'm sorry if there is something wrong. : bow_tone1:
//Make the adapter you want to use
MyAdapter adapter = new MyAdapter();
//Make PaneRecyclerViewAdapter
PaneRecyclerViewAdapter paneRecyclerViewAdapter = new PaneRecyclerViewAdapter(adapter); //I'll give you an adapter
//If it's GridView, I'll give you LayoutManager
//paneRecyclerViewAdapter.setGridLayoutManager(gridLayoutManager);
//Give a view of the header and footer
paneRecyclerViewAdapter.setHeaderView(headerView);
paneRecyclerViewAdapter.setFooterView(footerView);
//Add PaneRecyclerVieAdapter to RecyclerView
recyclerView.setAdapter(paneRecyclerViewAdapter);
Get the main adapter in the constructor. Basically, let this guy move sideways.
public PaneRecyclerViewAdapter(RecyclerView.Adapter adapter) {
mAdapter = adapter;
}
Sort the types appropriately.
@Override
public int getItemViewType(int position) {
if(isHeader(position)) {
return ITEM_VIEW_TYPE_HEADER;
}
if(isFooter(position)) {
return ITEM_VIEW_TYPE_FOOTER;
}
return ITEM_VIEW_TYPE_ITEM;
}
private boolean isHeader(int position) {
return mHeaderView != null && position == 0;
}
private boolean isFooter(int position) {
int extraCount = mHeaderView != null ? 1 : 0;
return mFooterView != null && position == mAdapter.getItemCount() + extraCount;
}
The number of items is returned by adding the number of headers and footers.
@Override
public int getItemCount() {
int extraCount = 0;
if(mHeaderView != null) extraCount += 1;
if(mFooterView != null) extraCount += 1;
return mAdapter.getItemCount() + extraCount;
}
ViewHolder, which is an ordinary item, is made by the Adapter you received.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == ITEM_VIEW_TYPE_HEADER) {
return new ViewHolder(mHeaderView);
}
if(viewType == ITEM_VIEW_TYPE_FOOTER) {
return new ViewHolder(mFooterView);
}
RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(parent, viewType);
return new ViewHolder(viewHolder);
}
By the way, the View Holder of Pane Recycler looks like the following. Ordinary items are kept in the raw ViewHolder type and handed over when needed. Header and Footer are already View, so it's a sideways parent.
static class ViewHolder extends RecyclerView.ViewHolder {
private RecyclerView.ViewHolder mItemViewHolder;
ViewHolder(View itemView) {
super(itemView);
}
ViewHolder(RecyclerView.ViewHolder viewHolder) {
super(viewHolder.itemView);
mItemViewHolder = viewHolder;
}
RecyclerView.ViewHolder getItemViewHolder() {
return mItemViewHolder;
}
}
OnBind is also left to the Adapter who received the ordinary item. HeaderFooter has nothing to do, so you shouldn't have to do anything
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(isHeader(position)) return;
if(isFooter(position)) return;
int extraCount = mHeaderView != null ? 1 : 0;
mAdapter.onBindViewHolder(holder.getItemViewHolder(), position - extraCount);
}
At the time of GridLayout, I want HeaderFooter to be full sideways, so SpanSizeLookup? It seems to be set with. Linear is full of people, so you don't have to do anything. I'm trying to make it easy to set up, but if you want to set it up outside, it's okay to do so. (If you use getSpanSize prepared below, you should be able to do it outside)
public void setGridLayoutManager(GridLayoutManager gridLayoutManager) {
mSpanCount = gridLayoutManager.getSpanCount();
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return PaneRecyclerViewAdapter.this.getSpanSize(position);
}
});
}
public int getSpanSize(int position) {
if(isHeader(position) || isFooter(position)) {
return mSpanCount;
}
return 1;
}
You shouldn't have done anything difficult. However, at that time, I made it while saying hi. Please let me know if you have any. Excuse me. : walking_tone1:
Github https://github.com/syuki/PaneRecyclerViewAdapter
Recommended Posts