RecyclerStickyHeaderView


Source link: https://github.com/TellH/RecyclerStickyHeaderView

RecyclerStickyHeaderView


Sticky header view or suspending view for RecyclerView.
StickyListHeaders is an Android library that makes it easy to integrate section headers stick to the top in ListView. Inspire by it, I setup this project to implement the same effect in RecyclerView.

Effect

Usage

Setup

root build.gradle

allprojects {

  repositories {

jcenter()

maven {
 url "https://jitpack.io" 
}

  
}
 
}

app build.gradle

dependencies {

 compile 'com.github.TellH:RecyclerStickyHeaderView:1.1.0' 
}

Quick Start

  • Place RecylerView into StickyHeaderView
 <tellh.com.stickyheaderview_rv.StickyHeaderView

android:id="@+id/stickyHeaderView"

android:layout_width="match_parent"

android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView

 android:id="@+id/recyclerView"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:background="@android:color/white"

 android:scrollbars="vertical" />
  </tellh.com.stickyheaderview_rv.StickyHeaderView> 
  • Create data bean class for each item type in RecyclerView. They should extend DataBean. Override the method
    public boolean shouldSticky() to decide whether the item view should be suspended on the top.
public class User extends DataBean {

  private String login;
  private int id;
  private String avatar_url;
  private boolean shouldSticky;
  @Override
  public int getItemLayoutId(StickyHeaderViewAdapter adapter) {

return R.layout.item_user;
  
}

  public void setShouldSticky(boolean shouldSticky) {

this.shouldSticky = shouldSticky;
  
}

  // Decide whether the item view should be suspended on the top.
  @Override
  public boolean shouldSticky() {

return shouldSticky;
  
}
 
}
 public class ItemHeader extends DataBean {

  private String prefix;
  @Override
  public int getItemLayoutId(StickyHeaderViewAdapter adapter) {

return R.layout.header;
  
}

  @Override
  public boolean shouldSticky() {

return true;
  
}
 
}
  • Create ViewBinder to bind different type views with specific data beans. As you see, provideViewHolder(View itemView) corresponds for onCreateViewHolder in RecyclerView, and bindView corresponds for onBindViewHolder in RecyclerView.
public class ItemHeaderViewBinder extends ViewBinder<ItemHeader, ItemHeaderViewBinder.ViewHolder> {

  @Override
  public ViewHolder provideViewHolder(View itemView) {

return new ViewHolder(itemView);

  
}

  @Override
  public void bindView(StickyHeaderViewAdapter adapter, ViewHolder holder, int position, ItemHeader entity) {

holder.tvPrefix.setText(entity.getPrefix());

  
}

  @Override
  public int getItemLayoutId(StickyHeaderViewAdapter adapter) {

return R.layout.header;
  
}

  static class ViewHolder extends ViewBinder.ViewHolder {

TextView tvPrefix;

public ViewHolder(View rootView) {

 super(rootView);

 this.tvPrefix = (TextView) rootView.findViewById(R.id.tv_prefix);

}

  
}
 
}
  • Instantiate StickyHeaderViewAdapter for RecyclerView and register ViewBinders for each item types.

  rv = (RecyclerView) findViewById(R.id.recyclerView);

rv.setLayoutManager(new LinearLayoutManager(this));

List<DataBean> userList = new ArrayList<>();

adapter = new StickyHeaderViewAdapter(userList)

  .RegisterItemType(new UserItemViewBinder())

  .RegisterItemType(new ItemHeaderViewBinder());

rv.setAdapter(adapter);

That is all.

Please check out the Demo and source code for more information. If you have any question, feel free to raise an issue. Thanks a lot!

Thanks

License

Copyright 2016 TellH

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

This application will scan through all available secret codes on your device. Then you will be able to execute these secret codes and discover hidden functionalities.

Install Referrer is an Open Source application that allows you to test the referrer attribute on the Google Play Store

Android reactive bluetooth library.

RxJava SharedPreferences for Android, lightweight and extendable.

Linear layout, that wrap its content to the next line if there is no space in the current line.

OpenALPR is an open source Automatic License Plate Recognition library.

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