RecyclerAdapters


Source link: https://github.com/alex-michenko/RecyclerAdapters

RecyclerAdapters

Introduction

RecyclerAdapters provides you a very simple way for implement RecyclerView.Adapter class in your projects. With this library you can implement Adapter in a few line.

Get started

dependencies {

  compile 'com.github.alex-michenko:recycleradapters:0.3.0' 
}

How to use

1.Create DataHolder

Create class-wrapper that will hold data for adapter:

public class PersonDH implements RecyclerDH {

 public boolean isFavourite;
public int countStars;
public String name;
 public String headerName;
 public PersonDH(String name, boolean isFavourite) {

 this.name = name;

 this.isFavourite = isFavourite;

  if (isFavourite)

  countStars = new Random().nextInt(4) + 1;

}

 public PersonDH(String headerName) {

 this.headerName = headerName;

}
 
}

2. Create ViewHolder

Create ViewHolder class and set generic parameter as created DataHolder:

public class PersonVH extends RecyclerVH<PersonDH> {

 private TextView tvNamePerson;
 public PersonVH(View itemView) {

 super(itemView);

  tvNamePerson = findView(R.id.tvNamePerson);

}

 @Override
public void setListeners(final OnCardClickListener listener) {

 tvNamePerson.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View view) {

 listener.onClick(view, getAdapterPosition(), getItemViewType());

  
}

 
}
);

}

 @Override
public void bindData(PersonDH data) {

 tvNamePerson.setText(data.name);

}
  
}

Method setListeners() is called if OnCardClickListener was setted for your adapter

If you adapter has several viewTypes then create suitable ViewHolders:

public class FavouriteVH extends RecyclerVH<PersonDH> {

//..... 
}
public class HeaderVH extends RecyclerVH<PersonDH> {

//..... 
}

3. Create adapter

Create an adapter and implement methods for cteating ViewHolders

public class PersonAdapter extends RecyclerAdapter<PersonDH> {

 private static final int HEADER = 1;
private static final int PERSON = 11;
private static final int FAVOURITE = 0;
 @NonNull
@Override
protected RecyclerVH<PersonDH> createVH(View view, int viewType) {

 switch (viewType) {

  case PERSON:

return new PersonVH(view);

  case HEADER:

return new HeaderVH(view);

  default:

return new FavouriteVH(view);

 
}

}

 @Override
protected int getLayoutRes(int viewType) {

 switch (viewType) {

  case PERSON:

return R.layout.item_person;

  case HEADER:

return R.layout.item_header;

  default:

return R.layout.item_favourite;

 
}

}

 @Override
public int getItemViewType(int position) {

 PersonDH dh = getItem(position);

 if (dh.headerName != null) {

  return HEADER;

 
}
 else {

  return dh.isFavourite ? FAVOURITE : PERSON;

 
}

}
 
}

4. Use adapter

Init your adapter and set in RecyclerView :


rvSimpleList = (RecyclerView) view.findViewById(R.id.rvTypedList);

 rvSimpleList.setLayoutManager(new LinearLayoutManager(getContext()));

  adapter = new PersonAdapter();

 adapter.setOnCardClickListener(new OnCardClickListener() {

  @Override

  public void onClick(View view, int position, int viewType) {

//do something

  
}

 
}
);

  rvSimpleList.setAdapter(adapter);

For more information see sample.

License

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

A small library which helps to use TabLayout with ViewPager more easily.

This is a specified proportion to the size of the Layout or View support library, with which you can easily set a fixed ratio of the size of the Layout or View, internal adaptive size calculation, completely abandon the code to calculate the size!

This is a picture select Android library that supports camera to take pictures, picture gallery multiple choice, image shearing!

This is an android in the Dialog library, not based on a systematic way to achieve Dialog using Activity root view of the Add Dialog, it has strong scalability and customizability.

Animated counting TextView component for Android. With CountingTextView you can enumerate a TextView with given start and end values.

This is a cool radio button group!

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