-
Notifications
You must be signed in to change notification settings - Fork 555
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);
In order to serve all the new functionalities, the Adapter list should be a @NonNull
copy(1) of the original List and must contain items of type IFlexible
.
Adapter can also be initialized with a null
or empty list: Then addItems()
or updateDataSet()
can be safely called, but it is strongly recommended to provide some items to the Constructor, otherwise scrolling animation on loading cannot be performed.
(1) = The copy of the list is necessary, it is manipulated for operations like filter, expanding/collapsing and others special operations.
Only the references of the items are copied, while the instances remain unique.
// This is a copy
new ArrayList<>(originalList);
FlexibleAdapter already supports multiple view types and with version 5.0.0 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.
Here we implement a simple item and its view holder as following:
/**
* Where AbstractFlexibleItem implements IFlexible!
*/
public class MyItem extends AbstractFlexibleItem<MyItem.MyViewHolder> {
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.
* 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.
* - Collapsing many expandable items is much faster.
* - 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").
*/
@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;
}
/**
* The Adapter is provided to be forwarded to the MyViewHolder.
* The unique instance of the LayoutInflater is also provided to simplify the
* creation of the VH.
*/
@Override
public MyViewHolder createViewHolder(FlexibleAdapter adapter, LayoutInflater inflater,
ViewGroup parent) {
return new MyViewHolder(inflater.inflate(getLayoutRes(), parent, false), adapter);
}
/**
* The Adapter and the Payload are provided to get more specific information from it.
*/
@Override
public void bindViewHolder(FlexibleAdapter adapter, MyViewHolder holder, int position,
List 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: 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
.
Note:
MyViewHolder
in turn extends theFlexibleViewHolder
, another utility class provided for easy setup with FlexibleAdapter. 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"?>
<android.support.v7.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>
</android.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(R) 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!