[JAVA] Introducing what I made when I wanted to add a header and footer to RecyclerView

Hello. I'm writing this kind of thing for the first time, so I don't know what to do, but thank you.

Synopsis

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:

How to use the Adapter you made

//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);

Lightly explain the movement

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;
}

end

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

Introducing what I made when I wanted to add a header and footer to RecyclerView
I wanted to animate a row when using realm and RecyclerView on Android, but I gave up
What I tried when I wanted to get all the fields of a bean
What I was addicted to when introducing the JNI library
What to do when a javax.el.PropertyNotWritableException occurs
I made a virtual currency arbitrage bot and tried to make money
I wanted to add @VisibleForTesting to the method
[Rails] [Memo] When to add = to <%%> and when not
A story when I tried to make a video by linking Processing and Resolume
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I made a Docker container to run Maven
What I did when I converted java to Kotlin
I want to issue a connection when a database is created using Spring and MyBatis
What I was addicted to when developing a Spring Boot application with VS Code
How to create a header or footer once and use it on another page
I tried what I wanted to try with Stream softly.
I made a method to ask for Premium Friday
[Ruby] I made a crawler with anemone and nokogiri.
What to do when a null byte error occurs
What to do when rails creates a 〇〇 2.rb file
I made a Restful server and client in Spring.
I want to add a reference type column later
I want to add a delete function to the comment function
I added a Jar file to the build path in an Eclipse project, but the situation and what to do when `java.util.zip.ZipException: invalid LOC header (bad signature)` appears.
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I made a plugin to execute jextract with Gradle task
I want to make a list with kotlin and java!
I want to call a method and count the number
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I tried JAX-RS and made a note of the procedure
Add a shadow to the Swift Button (and also the circle)
[Personal development] I made a site to recruit test users!
I made a Dockerfile to start Glassfish 5 using Oracle Java