SectionedRecyclerView


Source link: https://github.com/truizlop/SectionedRecyclerView

SectionedRecyclerView

An adapter to create Android RecyclerViews with sections, providing headers and footers.

Usage

In order to use this library, you need to extend SectionedRecyclerView<H, VH, F> where:

  • H is a class extending RecyclerView.ViewHolder to hold the view for section headers.
  • VH is a class extending RecyclerView.ViewHolder to hold the view for the regular items in the view.
  • F is a class extending RecyclerView.ViewHolder to hold the view for section footers.

According to the sample published in this repository:

    1. Create a class extending SectionedRecyclerView:
public class CountSectionAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder, 

  CountItemViewHolder, 

  CountFooterViewHolder>
    1. Implement the corresponding methods:
@Override protected int getItemCountForSection(int section) {

  return section + 1; 
}
  @Override protected int getSectionCount() {

  return 5; 
}
  @Override protected boolean hasFooterInSection(int section) {

  return true; 
}
  protected LayoutInflater getLayoutInflater(){

  return LayoutInflater.from(context);
 
}
  @Override protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {

  View view = getLayoutInflater().inflate(R.layout.view_count_header, parent, false);

  return new CountHeaderViewHolder(view);
 
}
  @Override protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {

  View view = getLayoutInflater().inflate(R.layout.view_count_footer, parent, false);

  return new CountFooterViewHolder(view);
 
}
  @Override protected CountItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {

  View view = getLayoutInflater().inflate(R.layout.view_count_item, parent, false);

  return new CountItemViewHolder(view);
 
}
  @Override protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {

  holder.render("Section " + (section + 1));
 
}
  @Override protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {

  holder.render("Footer " + (section + 1));
 
}
  protected int[] colors = new int[]{
0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800
}
; @Override protected void onBindItemViewHolder(CountItemViewHolder holder, int section, int position) {

  holder.render(String.valueOf(position + 1), colors[section]);
 
}
    1. If you use a GridLayoutManager, you need to set it a SectionedSpanSizeLookup to make sure that headers and footers span the whole width of the RecyclerView:
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
 SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(adapter, layoutManager);
 layoutManager.setSpanSizeLookup(lookup);
 recycler.setLayoutManager(layoutManager);
    1. Your result will look like this:

Even simpler

Most times you will need a simpler version of this adapter, where there are no footers and your headers will only be a title. For those cases, you have SimpleSectionedAdapter<VH>, where VH is a class extending ViewHolder to hold the view of the regular items in your RecyclerView.

In this case, you will have to implement the following methods:

@Override protected String getSectionHeaderTitle(int section) {

  return section == 0 ? "Today" : "Tomorrow"; 
}
  @Override protected int getSectionCount() {

  return 2; 
}
  @Override protected int getItemCountForSection(int section) {

  return 3; 
}
  @Override protected AgendaItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {

  LayoutInflater inflater = LayoutInflater.from(parent.getContext());

  View view = inflater.inflate(R.layout.view_agenda_item, parent, false);

  return new AgendaItemViewHolder(view);
 
}
  protected String[][] agenda = {
{
"Meeting", "Phone call", "Interview"
}
,

 {
"Basket match", "Grocery shopping", "Taking a nap"
}

}
;  @Override protected void onBindItemViewHolder(AgendaItemViewHolder holder, int section, int position) {

  holder.render(agenda[section][position]);
 
}

Your result will look like this:

Get it!

SectionedRecyclerView is available through JCenter. To be able to use this library in your project, add the following dependency to your build.gradle file:

dependencies{
  compile 'com.truizlop.sectionedrecyclerview:library:1.2.0' 
}

License

Copyright 2015 Tomás Ruiz-López  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

FcPermissions is a library to simplify basic system permissions logic when targeting Android M or higher.

A stylish music player.

Plugin which generates Android Parcelable boilerplate code for kotlin's data class.

Android Library to change drawable color dynamically.

Extension of RecyclerView.Adapter. You can add header/footer to RecyclerView for LinearLayoutManager and GridLayoutManager.

Some useful utility classes for Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes