Skip to content
Add Header and/or Footer in your RecyclerView in the simplest way possible.
Java Kotlin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
hfrecyclerview-example
hfrecyclerview-kotlin
hfrecyclerview
preview
.gitignore
LICENSE
README.md
build.gradle
dependencies.gradle
gradlew
gradlew.bat
settings.gradle

README.md

HFRecyclerView

sample

Android Arsenal Platform API Twitter

This is an Android library allowing to add Header and/or Footer in your RecyclerView in the simplest way possible.

Android app on Google Play

USAGE

To add Header and/or Footer in your RecyclerView you need to add HFRecyclerView library in your project or you can also grab it from Gradle:

implementation 'com.mikhaellopez:hfrecyclerview:1.1.1'

or KOTLIN version

implementation 'com.mikhaellopez:hfrecyclerview-kotlin:1.1.1'

KOTLIN

  1. You need to create a custom RecyclerView.Adapter for your RecyclerView which HFRecyclerView with the object type of your choice (in my example, my object type is MyDataObject). The first param in HFRecyclerView constructor is a flag to determine if you want to add a header, and the last to add a footer.

    class ExampleAdapter : HFRecyclerView<MyDataObject>(true, true) {
        //...
    }
  2. After that, override 3 methods and create 3 class which extend RecyclerView.ViewHolder in order to add the viewHolder for your Item, your Header and your Footer:

    class ExampleAdapter : HFRecyclerView<MyDataObject>(true, true) {
        
        //...
        
        //region Override Get ViewHolder
        override fun getItemView(inflater: LayoutInflater, parent: ViewGroup): RecyclerView.ViewHolder =
                ViewHolder.ItemViewHolder(inflater.inflate(R.layout.item_example, parent, false))
    
        override fun getHeaderView(inflater: LayoutInflater, parent: ViewGroup): RecyclerView.ViewHolder =
                ViewHolder.HeaderViewHolder(inflater.inflate(R.layout.item_header, parent, false))
    
        override fun getFooterView(inflater: LayoutInflater, parent: ViewGroup): RecyclerView.ViewHolder =
                ViewHolder.FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false))
        //endregion
        
        //region ViewHolder Header and Footer
        sealed class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    
            class ItemViewHolder(view: View) : ViewHolder(view) {
                fun bind(item: String) {
                    itemView.run { text.text = item }
                }
            }
    
            class HeaderViewHolder(view: View) : ViewHolder(view)
    
            class FooterViewHolder(view: View) : ViewHolder(view)
        }
        //endregion
    }

    ℹ️ If you doesn't have a footer (same for header) you need to override getFooterView like this:

    override fun getFooterView(inflater: LayoutInflater, parent: ViewGroup): RecyclerView.ViewHolder? = null
  3. You must override onBindViewHolder method to manage your views as you like:

    class ExampleAdapter : HFRecyclerView<MyDataObject>(true, true) {
    
        //...
    
        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            when (holder) {
                is ViewHolder.ItemViewHolder -> holder.bind(getItem(position))
                is ViewHolder.HeaderViewHolder -> { }
                is ViewHolder.FooterViewHolder -> { }
            }
        }
        
        //...
    }
  4. Finally, you can used your adapter and set yout data like this:

    val adapter = ExampleAdapter()
    adapter.data = youtDataList
    recyclerview.adapter = adapter

ℹ️ You can see a full example here : ExampleAdapter and MainActivity

JAVA

  1. You need to create a custom RecyclerView.Adapter for your RecyclerView which extends HFRecyclerView with the object type of your choice (in my example, my object type is MyDataObject) :

    public class ExampleAdapter extends HFRecyclerView<MyDataObject> {
        //...
    }
  2. In your custom adapter, add a constructor. The first param in super constructor is a flag to determine if you want to add a header, and the second is a flag to add a footer. You can also used an other super constructor with your data list in first param.

    public class ExampleAdapter extends HFRecyclerView<MyDataObject> {
    
        public ExampleAdapter() {
            // With Header & With Footer
            super(true, true);
        }
    
        //...
    }
  3. After that, override 3 methods and create 3 class which extend RecyclerView.ViewHolder in order to add the viewHolder for your Item, your Header and your Footer:

    public class ExampleAdapter extends HFRecyclerView<MyDataObject> {
        
        //...
        
        //region Override Get ViewHolder
        @NotNull
        @Override
        protected RecyclerView.ViewHolder getItemView(@NotNull LayoutInflater inflater, @NotNull ViewGroup parent) {
            return new ItemViewHolder(inflater.inflate(R.layout.item_example, parent, false));
        }
    
        @Override
        protected RecyclerView.ViewHolder getHeaderView(@NotNull LayoutInflater inflater, @NotNull ViewGroup parent) {
            return new HeaderViewHolder(inflater.inflate(R.layout.item_header, parent, false));
        }
    
        @Override
        protected RecyclerView.ViewHolder getFooterView(@NotNull LayoutInflater inflater, @NotNull ViewGroup parent) {
            return new FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false));
        }
        //endregion
        
        //region ViewHolder Header and Footer
        class ItemViewHolder extends RecyclerView.ViewHolder {
            TextView text;
    
            ItemViewHolder(View itemView) {
                super(itemView);
                text = (TextView)itemView.findViewById(R.id.text);
            }
            
            void bind(MyDataObject item) {
                text.setText(item.value());
            }
        }
    
        class HeaderViewHolder extends RecyclerView.ViewHolder {
            HeaderViewHolder(View itemView) {
                super(itemView);
            }
        }
    
        class FooterViewHolder extends RecyclerView.ViewHolder {
            FooterViewHolder(View itemView) {
                super(itemView);
            }
        }
        //endregion
    }
  4. You must override onBindViewHolder method to manage your views as you like:

    public class ExampleAdapter extends HFRecyclerView<MyDataObject> {
    
        //...
    
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ItemViewHolder) {
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                itemViewHolder.bind(getItem(position));
            } else if (holder instanceof HeaderViewHolder) {
    
            } else if (holder instanceof FooterViewHolder) {
    
            }
        }
        
        //...
    }
  5. Finally, you can used your adapter and set yout data like this:

    ExampleAdapter adapter = new ExampleAdapter();
    adapter.setData(youtDataList);
    recyclerView.setAdapter(adapter);

ℹ️ You can see a full example here : ExampleAdapter and MainActivity

LICENCE

CircularImageView by Lopez Mikhael is licensed under a Apache License 2.0. Based on a work at @hister.

You can’t perform that action at this time.