init RecyclerView Header Scroller - Android User Interface

Android examples for User Interface:RecyclerView

Description

init RecyclerView Header Scroller

Demo Code


import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;

public class Main{
    public static void initHeaderScroller(RecyclerView rv) {
        RecyclerView.Adapter adapter = rv.getAdapter();
        if (adapter instanceof IHeaderAdapter) {
            ((IHeaderAdapter) adapter).initScrollManager(getSpanSize(rv));
            initSpanSizeLookup(rv, (IHeaderAdapter) adapter);
        }/*from ww w.  ja  v  a  2  s . c  om*/
    }
    private static Integer getSpanSize(RecyclerView rv) {
        final RecyclerView.LayoutManager lm = rv.getLayoutManager();
        if (lm != null && lm instanceof GridLayoutManager)
            return ((GridLayoutManager) lm).getSpanCount();
        return 1;
    }
    public static boolean initSpanSizeLookup(final RecyclerView rv,
            final IHeaderAdapter adapter) {
        final RecyclerView.LayoutManager lm = rv.getLayoutManager();
        if (lm != null && lm instanceof GridLayoutManager) {
            ((GridLayoutManager) lm)
                    .setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                        @Override
                        public int getSpanSize(int position) {
                            if (rv == null)
                                return 1;
                            return adapter.isHeader(position) ? ((GridLayoutManager) lm)
                                    .getSpanCount() : 1;
                        }
                    });
            return true;
        }
        return false;
    }
}

Related Tutorials