Skip to content
Davide Steduto edited this page Nov 16, 2016 · 44 revisions

In this page


Project setup

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

Simple initialization

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);

Since the Adapter needs the items of type IFlexible interface in order to serve all the new functionalities, the list coming from getDatabaseList() should return a @NonNull copy(1) of the original List and must contain items of type IFlexible.

(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.

public List<Item> getDatabaseList() {
	return new ArrayList<>(originalList);
}

Note: Adapter can 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.

Implement and customize the item (MyItem and MyViewHolder classes)

To use FlexibleAdapter you're required to create the adapter's item class along with it's 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 next 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.
     */
    @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 the AbstractFlexibleItem, 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 of MyItem.

Note: MyViewHolder in turn extends the FlexibleViewHolder, another utility class provided for easy setup with FlexibleAdapter. It is recommended to extend this class to enable more advanced functionalities (described later).

Item layout

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;
}

Clone this wiki locally