RecyclerViewHelper


Source link: https://github.com/nisrulz/recyclerviewhelper

Android library that provides most common functions around recycler-view like Swipe to dismiss, Drag and Drop, Divider in the ui, events for when item selected and when not selected, on-click listener for items.

Built with ?? by Nishant Srivastava and contributors


Integration

RecyclerViewHelper is available in the Jcenter, so getting it as simple as adding it as a dependency

// Required , so make sure your support libs are updated in the android sdk implementation "com.android.support:appcompat-v7:{
latest version
}
' implementation "com.android.support:recyclerview-v7:{
latest version
}
'  // RecyclerViewHelper implementation 'com.github.nisrulz:recyclerviewhelper:{
latest version
}
'

where { latest version } corresponds to published version in

NOTE : The version here corresponds to the version of recyclerview dependency.

Make sure that the google's maven repo is declared in your projects build.gradle file as below
allprojects {

repositories {

  google()
  jcenter()

}
 
}

Usage

  • Implement the RHVAdapter in your recycler view adapter and RHVViewHolder in your ItemViewHolder
 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemViewHolder> implements RVHAdapter {

...

  @Override

public boolean onItemMove(int fromPosition, int toPosition) {

 swap(fromPosition, toPosition);

 return false;

}

  @Override

public void onItemDismiss(int position, int direction) {

 remove(position);

}

  public class ItemViewHolder extends RecyclerView.ViewHolder implements RVHViewHolder {

 ...

  @Override

 public void onItemSelected(int actionstate) {

  System.out.println("Item is selected");

 
}

@Override

 public void onItemClear() {

  System.out.println("Item is unselected");

}

}

  // Helper functions you might want to implement to make changes in the list as an event is fired

private void remove(int position) {

 dataList.remove(position);

 notifyItemRemoved(position);

}

  private void swap(int firstPosition, int secondPosition) {

 Collections.swap(dataList, firstPosition, secondPosition);

 notifyItemMoved(firstPosition, secondPosition);

}

  
}
 
  • Then implement your recycler view
public class MainActivity extends AppCompatActivity {

 RecyclerView myrecyclerview;

  ArrayList<String> data;

  MyAdapter adapter;

@Override

  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 myrecyclerview = (RecyclerView) findViewById(R.id.rv_fruits);

 data = new ArrayList<>();

data.add("Apple");

...

data.add("Fig");

 // Setup your adapter

adapter = new MyAdapter(data);

// Setup 

myrecyclerview.hasFixedSize();

myrecyclerview.setLayoutManager(new LinearLayoutManager(this));

myrecyclerview.setAdapter(adapter);

  // Setup onItemTouchHandler to enable drag and drop , swipe left or right

ItemTouchHelper.Callback callback = new RVHItemTouchHelperCallback(adapter, true, true,

  true);

ItemTouchHelper helper = new ItemTouchHelper(callback);

helper.attachToRecyclerView(myrecyclerview);

 // Set the divider in the recyclerview

myrecyclerview.addItemDecoration(new RVHItemDividerDecoration(this, LinearLayoutManager.VERTICAL));

 // Set On Click Listener

myrecyclerview.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {

 @Override

 public void onItemClick(View view, int position) {

  String value = "Clicked Item " + data.get(position) + " at " + position;

Log.d("TAG", value);

  Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();

 
}

}
));

}

 
}
  

Demo

Pull Requests

I welcome and encourage all pull requests. It usually will take me within 24-48 hours to respond to any issue or request. Here are some basic rules to follow to ensure timely addition of your request:

  1. Match coding style (braces, spacing, etc.) This is best achieved using CMD+Option+L (Reformat code) on Mac (not sure for Windows) with Android Studio defaults. The code style used in this project is from Grandcentrix, so please use the same when editing this project.
  2. If its a feature, bugfix, or anything please only change code to what you specify.
  3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :)
  4. Pull requests must be made against develop branch. Any other branch (unless specified by the maintainers) will get rejected.
  5. Check for existing issues first, before filing an issue.
  6. Have fun!

Author & support

This project was created by Nishant Srivastava but hopefully developed and maintained by many others. See the the list of contributors here.

Special Credits to Paul Burke and his article which got me thinking

This library contains a modified version of his implementations of ItemTouchHelper.


If you appreciate my work, consider buying me a cup of ☕? to keep me recharged 🤘

  • PayPal
  • Bitcoin Address: 13PjuJcfVW2Ad81fawqwLtku4bZLv1AxCL

I love using my work and I'm available for contract work. Freelancing helps to maintain and keep my open source projects up to date!

Resources

Custom animated pull-to-refresh that can be easily added to RecyclerView.

A bottom navigation view solution for Android.

SineView allows you to paint and animate a sine.

android-phone-field is A small UI library that allows you to create phone fields with corresponding country flags, and validate the phone number using libphonenumber from google.

Simplified android async HTTP client.

Convenience library to handle different view types with different presenters in a single RecyclerView.

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