Sectioned RecyclerView


Source link: https://github.com/IntruderShanky/Sectioned-RecyclerView

Sectioned-RecyclerView

Easy implementation of RecyclerView with headers and items

Demo App - IS Library

Demo

Usage

Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {

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

 
}
 
}
Step 2. Add the dependency
dependencies {

 compile 'com.github.IntruderShanky:Sectioned-RecyclerView:2.1.1' 
}

Implementation

There are very easy and simple steps to implement.

Step 1. Add RecyclerView in layout file:
<android.support.v7.widget.RecyclerView
 android:id="@+id/recycler_view"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
Step 2. Create another layout file for Section Header:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingTop="26dp"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:orientation="vertical">

<TextView

android:id="@+id/section"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="16sp"

android:textSize="16sp"

android:textStyle="bold"/>
  <View

android:layout_width="match_parent"

android:layout_height="1dp"

android:layout_marginTop="5dp"

android:background="#88424242"/> </LinearLayout>
Step 3. Create another layout file for Section Child:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="8dp"
  android:orientation="vertical">

<TextView

android:id="@+id/child"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="16sp"

android:text="mc sjcnscdj "

android:padding="8dp"

android:fontFamily="sans-serif-condensed"/>  </LinearLayout>
Step 4. Create a class for Section Child and named it. Here "Child.java"
public class Child {

String name;

public Child(String name) {

this.name = name;
  
}

public String getName() {

return name;
  
}
 
}
Step 5. Create a class for Section Header and implement it with Section. Here "Section.java"
public class SectionHeader implements Section<Child> {

List<Child> childList;
  String sectionText;

public SectionHeader(List<Child> childList, String sectionText) {

this.childList = childList;

this.sectionText = sectionText;
  
}

@Override
  public List<Child> getChildItems() {

return childList;
  
}

public String getSectionText() {

return sectionText;
  
}
 
}
Step 6. Create a two ViewHolder classes to hold the views of section and child.
public class SectionViewHolder extends RecyclerView.ViewHolder {

TextView name;
  public SectionViewHolder(View itemView) {

super(itemView);

 name = (TextView) itemView.findViewById(R.id.section);

  
}
 
}
public class ChildViewHolder extends RecyclerView.ViewHolder {

TextView name;
  public ChildViewHolder(View itemView) {

super(itemView);

name = (TextView) itemView.findViewById(R.id.child);

  
}
 
}
Step 7. Create a Adater for RecyclerView. Here "AdapterSectionRecycler.java"
public class AdapterSectionRecycler extends SectionRecyclerViewAdapter<SectionHeader, Child, SectionViewHolder, ChildViewHolder> {

Context context;

public AdapterSectionRecycler(Context context, List<SectionHeader> sectionItemList) {

super(context, sectionItemList);

this.context = context;
  
}

@Override
  public SectionViewHolder onCreateSectionViewHolder(ViewGroup sectionViewGroup, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.section_item, sectionViewGroup, false);

return new SectionViewHolder(view);

  
}

@Override
  public ChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.item_layout, childViewGroup, false);

return new ChildViewHolder(view);

  
}

@Override
  public void onBindSectionViewHolder(SectionViewHolder sectionViewHolder, int sectionPosition, SectionHeader section) {

sectionViewHolder.name.setText(section.sectionText);

  
}

@Override
  public void onBindChildViewHolder(ChildViewHolder childViewHolder, int sectionPosition, int childPosition, Child child) {

childViewHolder.name.setText(child.getName());

  
}
 
}
Step 8. Here complete code of your Activity Class, Here "MainActivity.java"
public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
  AdapterSectionRecycler adapterRecycler;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 //initialize RecyclerView

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

 //setLayout Manager

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

recyclerView.setLayoutManager(linearLayoutManager);

recyclerView.setHasFixedSize(true);

 //Create a List of Child DataModel

List<Child> childList = new ArrayList<>();

childList.add(new Child("April"));

childList.add(new Child("Austin"));

childList.add(new Child("Alex"));

childList.add(new Child("Aakash"));

 //Create a List of Section DataModel implements Section

List<SectionHeader> sections = new ArrayList<>();

sections.add(new SectionHeader(childList, "A"));

 childList = new ArrayList<>();

childList.add(new Child("Bill Gates"));

childList.add(new Child("Bob Proctor"));

childList.add(new Child("Bryan Tracy"));

sections.add(new SectionHeader(childList, "B"));

 childList = new ArrayList<>();

childList.add(new Child("Intruder Shanky"));

childList.add(new Child("Invincible Vinod"));

sections.add(new SectionHeader(childList, "I"));

 childList = new ArrayList<>();

childList.add(new Child("Jim Carry"));

sections.add(new SectionHeader(childList, "J"));

 childList = new ArrayList<>();

childList.add(new Child("Neil Patrick Harris"));

sections.add(new SectionHeader(childList, "N"));

 childList = new ArrayList<>();

childList.add(new Child("Orange"));

childList.add(new Child("Olive"));

sections.add(new SectionHeader(childList, "O"));

 adapterRecycler = new AdapterSectionRecycler(this, sections);

recyclerView.setAdapter(adapterRecycler);

  
}
 
}

Data Change Methods

insertNewSection(Section) insertNewSection(Section, int) removeSection(int) insertNewChild(Object, int) insertNewChild(Object, int, int) removeChild(int, int)  /* To notify the adapter for data change   * call notifyDataChanged(List) instead of   * notiftDatasetChanged()  */  notifyDataChanged(List)

Licence

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

Yahoo Weather Data.

Dredd was created to be a simple way to detach application business logic in order to create a decision tree model best for visualize and perhaps easy to understand and maintain.

Fluxxan Fluxxan is an Android implementation of the Flux Architecture that combines concepts from both Fluxxor and Redux.

Meter is a simple micro-benchmarking tool for Android (and Java) projects. This is not a profiler, this is very small utility class that designed for making benchmarking easy.

This is an ambitious example project of what can be done with RxJava to create an app based on streams of data.

Material CAB allows you to implement a customizable and flexible contextual action bar in your app. The traditional stock CAB on Android is limited to being placed at the top of your Activity, and the navigation drawer cannot go over it. This library lets you choose its exact location, and a toolbar is used allowing views to be be placed over and under it.

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