Skip to content

adapter

boybeak edited this page Aug 6, 2018 · 2 revisions

This adapter supports and bases on Data Binding. Please enable data binding as android official tutorial.

DataBindingAdapter

This is the most important section of this tutorial. DataBindingAdapter is a subclass of RecyclerView's Adapter. It must work with BaseLayoutImpl and AbsDataBindingHolder.

//Init the adapter
private var adapter: DataBindingAdapter? = null
....
//In activity or fragment's onCreate
adapter = DataBindingAdapter(context)

Define a subclass of BaseLayoutImpl Footer.kt

public class FooterImpl extends BaseLayoutImpl<Footer, FooterHolder> {

    public FooterImpl(Footer footer) {
        super(footer);
    }

    @Override
    public Class<FooterHolder> getHolderClass() {
        return FooterHolder.class;
    }

    @Override
    public int getLayout() {
        return R.layout.layout_footer;
    }

}

layout_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="footer"
            type="com.github.boybeak.starter.adapter.footer.Footer"/>
    </data>
    <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            app:footerState="@{footer.state}"
            app:footerMessage="@{footer.message}"
            >
            <ProgressBar
                android:id="@+id/footer_pb"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerInParent="true"
                android:visibility="gone"
                android:padding="8dp"
                style="@style/Widget.AppCompat.ProgressBar"/>
            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/footer_msg"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:fontFamily="@string/font_family_thin"
                android:textStyle="italic"
                android:textSize="18dp"
                android:gravity="center"
                android:layout_centerInParent="true"
                android:visibility="gone"
                />
        </RelativeLayout>
    </RelativeLayout>
</layout>

FooterHolder.kt

public class FooterHolder extends AbsDataBindingHolder<FooterImpl, LayoutFooterBinding> {

    public FooterHolder(@NotNull LayoutFooterBinding binding) {
        super(binding);
    }

    @Override
    public void onBindData(@NonNull Context context, @NonNull FooterImpl layout, int position, @NonNull RecyclerView.Adapter adapter) {
        binding().setFooter(layout.getSource());
    }
}

Once you wanna add a footer, do as below:

val footer = Footer()
adapter?.addFooter(FooterImpl(footer)).autoNotify() // What's **autoNotify**? See [this](https://github.com/boybeak/Starter/wiki/_new#datachange).

Actually you don't need to do this, because a build-in FooterAdapter is already prepared for you.

FooterAdapter

This is a build-in styled footer adapter. This can satisfy most of your need. The build-in Footer class has 4 states: LOADING, SUCCESS, FAILED and EMPTY. You can change the state by 4 methods of FooterAdapter: notifyLoadingFooter, notifySuccessFooter, notifyFailedFooter and notifyEmptyFooter.

DataChange

This is a replacement of adapter's notify-(Item, Range)-(Insert, Remove, Change). Just call autoNotify when using DataBindingAdapter if it can.

Clone this wiki locally