NoAdapter


Source link: https://github.com/tikivn/NoAdapter

NoAdapter

Too much boilerplate and effort to implement a list using RecyclerView. But, most of them can be omitted. This library will help you to focus on what should be display on the list instead of how.

  • Eliminate boilerplate code to create Adapter.
  • Eliminate boilerplate code to select view type and view holder.
  • Eliminate boilerplate code to calculate diff to update the list.
  • Diff calculation will be performed on background thread when list have more than 50 items.
  • And many more.

Table of contents

Demo

Why NoAdapter

  • You'll never have to implement Adapter for RecyclerView anymore.
  • Never worry about notifyChanged anymore. We used DiffUtil to calculate and notify changes for you.

Single View Type

1. Implement ViewHolder

item_text.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="8dp"
app:cardElevation="4dp">
 <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:textAppearance="@style/Base.TextAppearance.AppCompat.Body2"
/> </android.support.v7.widget.CardView>

TextViewHolder.java

public class TextViewHolder extends AbsViewHolder {

private final TextView text;
 private TextViewHolder(View view) {

  super(view);

  text = ((TextView) view.findViewById(R.id.text));

  // Set "this" to clickListener then it'll be delegated to Adapter
  view.setOnClickListener(this);

}

 public static TextViewHolder create(ViewGroup parent) {

  final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

  final View view = inflater.inflate(R.layout.item_text, parent, false);

  return new TextViewHolder(view);

}

 @Override public void bind(Object item) {

  super.bind(item);

  text.setText((String) item);

}
 
}

2. Setup Adapter

adapter = new OnlyAdapter.Builder()
  .viewHolderFactory(new ViewHolderFactory() {

 @Override public AbsViewHolder viewHolderForType(ViewGroup parent, int type) {

return TextViewHolder.create(parent);

 
}

  
}
)
  .onItemClickListener(new OnItemClickListener() {

 @Override public void onItemClick(View view, Object item, int position) {

Toast

 .makeText(MainActivity.this, "Clicked on item: " + item, Toast.LENGTH_SHORT)

 .show();

 
}

  
}
)
  .build();
 rvList.setAdapter(adapter);

3. Bind data

final List<String> items = Arrays.asList(
  "Text #1",
  "Text #2",
  "Text #3",
  "Text #4",
  "Text #5" );
 adapter.setItems(items);

Multiple View Type

1. ViewHolder

1.1 Color item

item_color.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="60dp"
android:layout_margin="8dp"
app:cardElevation="4dp"/>

ColorViewHolder.java

public class ColorViewHolder extends AbsViewHolder {

 private ColorViewHolder(View view) {

  super(view);

  // Set "this" to clickListener then it'll delegate to Adapter
  view.setOnClickListener(this);

}

 public static ColorViewHolder create(ViewGroup parent) {

  final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

  final View view = inflater.inflate(R.layout.item_color, parent, false);

  return new ColorViewHolder(view);

}

 @Override public void bind(Object item) {

  super.bind(item);

  // Set up display for data
  itemView.setBackgroundColor(((Color) item).getValue());

}
 
}

1.2. Text item

item_text.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="8dp"
app:cardElevation="4dp">
 <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:textAppearance="@style/Base.TextAppearance.AppCompat.Body2"
/> </android.support.v7.widget.CardView>

TextViewHolder.java

public class TextViewHolder extends AbsViewHolder {

private final TextView text;
 private TextViewHolder(View view) {

  super(view);

  text = ((TextView) view.findViewById(R.id.text));

  // Set "this" to clickListener then it'll be delegated to Adapter
  view.setOnClickListener(this);

}

 public static TextViewHolder create(ViewGroup parent) {

  final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

  final View view = inflater.inflate(R.layout.item_text, parent, false);

  return new TextViewHolder(view);

}

 @Override public void bind(Object item) {

  super.bind(item);

  text.setText((String) item);

}
 
}

2. Setup Adapter

adapter = new OnlyAdapter.Builder()
  .typeFactory(new TypeFactory() {

 @Override public int typeOf(Object item) {

return item instanceof Color ? 1 : 0;

 
}

  
}
)
  .viewHolderFactory(new ViewHolderFactory() {

 @Override public AbsViewHolder viewHolderForType(ViewGroup parent, int type) {

switch (type) {

  case 1:

 return ColorViewHolder.create(parent);

  default:

 return TextViewHolder.create(parent);

}

 
}

  
}
)
  .onItemClickListener(new OnItemClickListener() {

 @Override public void onItemClick(View view, Object item, int position) {

Toast

 .makeText(MainActivity.this, "Clicked on item: " + item, Toast.LENGTH_SHORT)

 .show();

 
}

  
}
)
  .diffCallback(new DiffCallback() {

 @Override public boolean areItemsTheSame(Object oldItem, Object newItem) {

if (oldItem instanceof Color) {

  return newItem instanceof Color

&& ((Color) oldItem).getId() == ((Color) newItem).getId();

}
 else {

  return oldItem.equals(newItem);

}

 
}

  @Override public boolean areContentsTheSame(Object oldItem, Object newItem) {

return oldItem.equals(newItem);

 
}

  
}
)
  .build();
 rvList.setAdapter(adapter);

3. Bind data

final List<Object> items = Arrays.asList(
  new Color(1, randomColor()),
  new Color(2, randomColor()),
  "Text #3",
  new Color(4, randomColor()),
  "Text #5" );
 adapter.setItems(items);

Use with Data Binding

1. Implement Layout + Binding

item_text.xml

<?xml version="1.0" encoding="utf-8"?> <layout> <data>
<variable
  name="item"
  type="java.lang.String"/>
<variable
  name="onClick"
  type="android.view.View.OnClickListener"/> </data> <android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="8dp"
android:onClick="@{
onClick
}
"
app:cardElevation="4dp">
 <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:text="@{
item
}
"
  android:textAppearance="@style/Base.TextAppearance.AppCompat.Body2"
/> </android.support.v7.widget.CardView> </layout>

NOTE:

  • item, onClick are predefined
  • item represented for data
  • onClick represented for onClickListener

2. Setup Adapter

adapter = new BindingBuilder()

.layoutFactory(new LayoutFactory() {

  @Override public int layoutOf(Object item) {

 return R.layout.item_text;

  
}

}
)

.onItemClickListener(new OnItemClickListener() {

  @Override public void onItemClick(View view, Object item, int position) {

 Toast

  .makeText(MainActivity.this, "Clicked on item: " + item, Toast.LENGTH_SHORT)

  .show();

  
}

}
)

.build();
 rvList.setAdapter(adapter);

3. Bind data

final List<String> items = Arrays.asList(
  "Text #1",
  "Text #2",
  "Text #3",
  "Text #4",
  "Text #5" );
 adapter.setItems(items);

See more in the sample

Download

Download the latest JAR or grab via Gradle:

compile 'vn.tiki.noadapter2:noadapter:2.0.2' // Use with data binding compile 'vn.tiki.noadapter2:noadapter-databinding:2.0.2'

Snapshots of the development version are available in Sonatype's snapshots repository.

ProGuard

No specific

License

Copyright 2016 Tiki Corp  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 simple line through text view. ie. ----- Hello, World -----.

A library that gives you access to the powerful Parse cloud platform from your Android app. For more information Parse and its features.

A simple asynchronous RSS 2.0 reader library for Android.

An extension for Google's AutoValue that omits @Redacted field values from toString().

A collection of source code generators for Java.

Subprojects:

  • AutoFactory - JSR-330-compatible factories
  • AutoService - Provider-configuration files for ServiceLoader
  • AutoValue - Immutable value-type code generation for Java 1.6+.
  • Common - Helper utilities for writing annotation processors.

CAT

A logging library for Android with focus on convenient usage and extensibility.

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