ItemPool


Source link: https://github.com/nekocode/ItemPool

ItemPool

Decouple the item(/nested viewholder) from recyclerview's adapter.

Reuse itemview in every recyclerview.

Install

repositories {

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

  compile 'com.github.nekocode:ItemPool:{
lastest-version
}
' 
}

Comparison

ItemPool Adapter
Write Adapter’s boilerplate code No Yes
Maintain an extra data list No Yes
Reuse item view in other RecyclerView Very easy Difficult
Handle view events Easy Difficult

Usage

Firstly, create a new Item (It's a bit similar to the ViewHolder). Override the onCreateItemView() method to create the view for this item. And override the onBindData() method for binding the corresponding type (the generic type of the class) of data to the item view.

public class TextItem extends Item<String> {

  private TextView textView;

@NonNull
  @Override
  public View onCreateItemView(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {

final View itemView = inflater.inflate(R.layout.item_test, parent, false);

textView = (TextView) itemView.findViewById(R.id.textView);

return itemView;
  
}

@Override
  public void onBindData(@NonNull String s) {

textView.setText(s);

  
}
 
}

And then obtain an ItemPool instance. It extends the ArrayList<Object> so you can add any data object to it. And for telling the itempool which Item will show in the recyclerview, you need to add classes of items.

ItemPool itemPool = new ItemPool();
 itemPool.addType(HeaderItem.class);
 itemPool.addType(TextItem.class);
  itemPool.add(new Header());
 itemPool.add("A");
 itemPool.add("B");

Set the internal adapter of the itempool to the recyclerview.

recyclerView.setAdapter(itemPool.getAdapter());

The itempool just like a mixture of data list and adapter because it has the notifyXXX() methods for refreshing the recyclerview.

itemPool.notifyDataSetChanged();

That's all! You don't need create Adapter any more! And every Item you create can be reused in any new recyclerview!

Handle view event

If you want to handle the item's view events. You can set an ItemEventHandler for a Item:

itemPool.onEvent(HeaderItem.class, new ItemEventHandler() {

  @Override
  public void onEvent(@NonNull ItemEvent event) {

switch (event.getAction()) {

 case Item.EVENT_ITEM_CLICK:

  Toast.makeText(MainActivity.this,

 "You just clicked the header.", Toast.LENGTH_SHORT).show();

  break;

  case HeaderItem.EVENT_TEXT_CLICK:

  Toast.makeText(MainActivity.this,

 "You just clicked the TextView.", Toast.LENGTH_SHORT).show();

  break;

}

  
}
 
}
);

It will trigger the ItemEvent.ITEM_CLICK events automatically. But you need to manually trigger other view events that you want to handle. For example:

public class HeaderItem extends Item<Header> implements View.OnClickListener {

  public static final int EVENT_TEXT_CLICK = 1;

private TextView textView;

// ...

@NonNull
  @Override
  public View onCreateItemView(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {

final View itemView = inflater.inflate(R.layout.item_test2, parent, false);

textView = (TextView) itemView.findViewById(R.id.textView);

textView.setOnClickListener(this);

return itemView;
  
}

@Override
  public void onClick(View v) {

if (v == textView) {

 event(EVENT_TEXT_CLICK, null);

}

  
}
 
}

Resources

ScrollParallaxImageView extends ImageView, and provides parallax effects when it scrolls in the screen.It can be use in any view which can scroll its content, like ListView, RecyclerView, ScrollView, etc.

A library designated for selecting an image via gallery or camera, with cropping abilities, in an easy way.

PageLoader is a simple android library for loading page with easy customization.

A flexible view for providing a limited rect window into a large data set, just like a two-dimensional RecyclerView.

ProbeTools displays the data from the Database and SharedPreferences in the browser.

Showcases different components that come with the Leanback library for Android TV.

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