Search in sources :

Example 1 with ExpandableGridSpacingDecorator

use of com.instructure.speedgrader.decorations.ExpandableGridSpacingDecorator in project instructure-android by instructure.

the class ParentFragment method configureRecyclerViewAsGrid.

public void configureRecyclerViewAsGrid(View rootView, final BaseRecyclerAdapter baseRecyclerAdapter, int swipeRefreshLayoutResId, int emptyViewResId, int recyclerViewResId, int emptyViewStringResId, final int span, View.OnClickListener emptyImageListener, Drawable... emptyImage) {
    final int cardPadding = getResources().getDimensionPixelOffset(R.dimen.card_outer_margin);
    EmptyViewInterface emptyViewInterface = (EmptyViewInterface) rootView.findViewById(emptyViewResId);
    final PandaRecyclerView recyclerView = (PandaRecyclerView) rootView.findViewById(recyclerViewResId);
    emptyViewInterface.emptyViewText(emptyViewStringResId);
    emptyViewInterface.setNoConnectionText(getString(R.string.noConnection));
    if (emptyImage != null && emptyImage.length > 0) {
        emptyViewInterface.emptyViewImage(emptyImage[0]);
        if (emptyImageListener != null && emptyViewInterface.getEmptyViewImage() != null) {
            emptyViewInterface.getEmptyViewImage().setOnClickListener(emptyImageListener);
        }
    }
    GridLayoutManager layoutManager = new GridLayoutManager(getContext(), span, GridLayoutManager.VERTICAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

        @Override
        public int getSpanSize(int position) {
            if (position < recyclerView.getAdapter().getItemCount()) {
                int viewType = recyclerView.getAdapter().getItemViewType(position);
                if (Types.TYPE_HEADER == viewType || PaginatedRecyclerAdapter.LOADING_FOOTER_TYPE == viewType) {
                    return span;
                }
            } else {
                // if something goes wrong it will take up the entire space, but at least it won't crash
                return span;
            }
            return 1;
        }
    });
    if (mSpacingDecoration != null) {
        recyclerView.removeItemDecoration(mSpacingDecoration);
    }
    if (baseRecyclerAdapter instanceof ExpandableRecyclerAdapter) {
        mSpacingDecoration = new ExpandableGridSpacingDecorator(cardPadding);
    } else {
        mSpacingDecoration = new GridSpacingDecorator(cardPadding);
    }
    recyclerView.addItemDecoration(mSpacingDecoration);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new ExpandCollapseItemAnimator());
    recyclerView.setEmptyView(emptyViewInterface);
    recyclerView.setAdapter(baseRecyclerAdapter);
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(swipeRefreshLayoutResId);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            if (!com.instructure.pandautils.utils.Utils.isNetworkAvailable(getContext())) {
                mSwipeRefreshLayout.setRefreshing(false);
            } else {
                baseRecyclerAdapter.refresh();
            }
        }
    });
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) GridSpacingDecorator(com.instructure.speedgrader.decorations.GridSpacingDecorator) ExpandableGridSpacingDecorator(com.instructure.speedgrader.decorations.ExpandableGridSpacingDecorator) PandaRecyclerView(com.instructure.pandarecycler.PandaRecyclerView) ExpandCollapseItemAnimator(com.instructure.speedgrader.animators.ExpandCollapseItemAnimator) EmptyViewInterface(com.instructure.pandarecycler.interfaces.EmptyViewInterface) ExpandableGridSpacingDecorator(com.instructure.speedgrader.decorations.ExpandableGridSpacingDecorator) ExpandableRecyclerAdapter(com.instructure.speedgrader.adapters.ExpandableRecyclerAdapter) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) Point(android.graphics.Point)

Aggregations

Point (android.graphics.Point)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 PandaRecyclerView (com.instructure.pandarecycler.PandaRecyclerView)1 EmptyViewInterface (com.instructure.pandarecycler.interfaces.EmptyViewInterface)1 ExpandableRecyclerAdapter (com.instructure.speedgrader.adapters.ExpandableRecyclerAdapter)1 ExpandCollapseItemAnimator (com.instructure.speedgrader.animators.ExpandCollapseItemAnimator)1 ExpandableGridSpacingDecorator (com.instructure.speedgrader.decorations.ExpandableGridSpacingDecorator)1 GridSpacingDecorator (com.instructure.speedgrader.decorations.GridSpacingDecorator)1