-
Notifications
You must be signed in to change notification settings - Fork 546
5.x | Setting Up
Before starting to use the FlexibleAdapter make sure you linked it to your project. You can find more info on setup here: https://github.com/davideas/FlexibleAdapter#setup
Initialization is done as following:
// Optional but strongly recommended: Compose the initial list
List<IFlexible> myItems = getDatabaseList();
// Initialize the Adapter
FlexibleAdapter<IFlexible> adapter = new FlexibleAdapter<>(myItems);
// Initialize the RecyclerView and attach the Adapter to it as usual
recyclerView.setAdapter(adapter);
It is recommended to provide some items to the Constructor, otherwise scrolling animation on loading cannot be performed. However, FlexibleAdapter can also be initialized with a null
or empty list: Then addItems()
or updateDataSet()
can be safely called.
⚠️ Warning: From 5.0.0-RC2, the copy of the list is now made internally. Only the references of the items are copied, while the instances remain unique. If the copy was provided in the past, now you should remove it during the constructor, updateDataSet and filterItems.
FlexibleAdapter already supports multiple view types and with version 5 you're required to create the Adapter's item class along with its view holder, both needing to extend certain FlexibleAdapter's interfaces.
- For a complete knowledge and advanced combinations, please see Item interfaces and their abstract implementation.
- See also ViewHolders for advanced view configuration.
Here we implement a simple item and its view holder as following:
/**
* Where AbstractFlexibleItem implements IFlexible!
*/
public class MyItem/*(1)*/ extends AbstractFlexibleItem<MyItem.MyViewHolder/*(2)*/> {
private String id;
private String title;
public MyItem(String id, String title) {
this.id = id;
this.title = title;
}
/**
* When an item is equals to another?
* Write your own concept of equals, mandatory to implement or use
* default java implementation (return this == o;) if you don't have unique IDs!
* This will be explained in the "Item interfaces" Wiki page.
*/
@Override
public boolean equals(Object inObject) {
if (inObject instanceof MyItem) {
MyItem inItem = (MyItem) inObject;
return this.id.equals(inItem.id);
}
return false;
}
/**
* You should implement also this method if equals() is implemented.
* This method, if implemented, has several implications that Adapter handles better:
* - The Hash, increases performance in big list during Update & Filter operations.
* - You might want to activate stable ids via Constructor for RV, if your id
* is unique (read more in the wiki page: "Setting Up Advanced") you will benefit
* of the animations also if notifyDataSetChanged() is invoked.
*/
@Override
public int hashCode() {
return id.hashCode();
}
/**
* For the item type we need an int value: the layoutResID is sufficient.
*/
@Override
public int getLayoutRes() {
return R.layout.item_flexible;
}
/**
* Delegates the creation of the ViewHolder to the user (AutoMap).
* The inflated view is already provided as well as the Adapter.
*/
@Override
public MyViewHolder createViewHolder(View view, FlexibleAdapter<IFlexible> adapter) {
return new MyViewHolder(view, adapter);
}
/**
* The Adapter and the Payload are provided to perform and get more specific
* information.
*/
@Override
public void bindViewHolder(FlexibleAdapter<IFlexible> adapter, MyViewHolder holder,
int position,
List<Object> payloads) {
holder.mTitle.setText(title);
// Title appears disabled if item is disabled
holder.mTitle.setEnabled(isEnabled());
}
/**
* The ViewHolder used by this item.
* Extending from FlexibleViewHolder is recommended especially when you will use
* more advanced features.
*/
public class MyViewHolder extends FlexibleViewHolder {
public TextView mTitle;
public MyViewHolder(View view, FlexibleAdapter adapter) {
super(view, adapter);
mTitle = (TextView) view.findViewById(R.id.title);
}
}
}
ℹ️ Note: Keep in mind that those items are mainly to be meant as presentation items.
1️⃣ = The definition of
MyItem
extends theAbstractFlexibleItem
, an abstract utility class which requires the type of the view holder you'd be using passed as a generic parameter. Despite this example,MyViewHolder
doesn't need to be declared as a inner class ofMyItem
.2️⃣ =
MyViewHolder
in turn extends theFlexibleViewHolder
, another predefined class provided for easy setup some click listeners and others item callbacks. It is recommended to extend this class to enable more advanced functionalities (described later).
Within the getLayoutRes
function you specify the item's layout, (layout/item_flexible.xml) for instance:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
⚠️ Warning: If using a version of FlexibleAdapter older than 5.1.0, replace both instances ofandroidx.cardview.widget.CardView
withandroid.support.v7.widget.CardView
.
Having MyItem declared, you can finally use FlexibleAdapter.
To see the FlexibleAdapter working, you may temporarily populate the item list by modifying the getDatabaseList
as follows:
public List<IFlexible> getDatabaseList() {
List<IFlexible> list = new ArrayList<>();
list.add(new MyItem("1", "Hello"));
list.add(new MyItem("2", "World"));
return list;
}
Or alternatively you can run the demo App from the Android Studio®️ project. A wiki page for the app is available here to guide you through it.
- Update Data Set
- Selection modes
- Headers and Sections
- Scrollable Headers and Footers
- Expandable items
- Drag&Drop and Swipe
- EndlessScroll / On Load More
- Search Filter
- FastScroller
- Adapter Animations
- Third party Layout Managers
- Payload
- Smooth Layout Managers
- Flexible Item Decoration
- Utils
- ActionModeHelper
- AnimatorHelper
- EmptyViewHelper
- UndoHelper
* = Under revision!