Search in sources :

Example 6 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project FlexibleAdapter by davideas.

the class FlexibleLayoutManager method findLastVisibleItemPosition.

/**
 * Helper method to find the adapter position of the <b>last partially</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>last partially</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findLastCompletelyVisibleItemPosition()
 * @since 5.0.0-rc1
 */
@Override
public int findLastVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findLastVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findLastVisibleItemPositions(null)[i];
            if (nextPosition > position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
    }
}
Also used : RecyclerView(androidx.recyclerview.widget.RecyclerView) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 7 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project FlexibleAdapter by davideas.

the class FlexibleLayoutManager method findFirstCompletelyVisibleItemPosition.

/**
 * Helper method to find the adapter position of the <b>first completely</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>first fully</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findFirstVisibleItemPosition()
 * @since 5.0.0-b8
 */
@Override
public int findFirstCompletelyVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findFirstCompletelyVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findFirstCompletelyVisibleItemPositions(null)[i];
            if (nextPosition < position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
    }
}
Also used : RecyclerView(androidx.recyclerview.widget.RecyclerView) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 8 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project FlexibleAdapter by davideas.

the class FlexibleItemDecoration method getItemOffsets.

/*====================*/
/* OFFSET CALCULATION */
/*====================*/
/**
 * @since 1.0.0-b1
 */
@Override
public void getItemOffsets(final Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
    int position = recyclerView.getChildAdapterPosition(view);
    // Skip check so on item deleted, offset is kept (only if general offset was set!)
    // if (position == RecyclerView.NO_POSITION) return;
    // Get custom Item Decoration or default
    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    int itemType = (position != RecyclerView.NO_POSITION ? adapter.getItemViewType(position) : 0);
    ItemDecoration deco = getItemDecoration(itemType);
    // No offset set, applies the general offset to this item decoration
    if (!deco.hasOffset()) {
        deco = new ItemDecoration(mOffset);
    }
    // Default values (LinearLayout)
    int spanIndex = 0;
    int spanSize = 1;
    int spanCount = 1;
    int orientation = RecyclerView.VERTICAL;
    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        spanIndex = lp.getSpanIndex();
        spanSize = lp.getSpanSize();
        GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
        // Assume that there are spanCount items in this row/column.
        spanCount = lm.getSpanCount();
        orientation = lm.getOrientation();
    } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        spanIndex = lp.getSpanIndex();
        StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
        // Assume that there are spanCount items in this row/column.
        spanCount = lm.getSpanCount();
        spanSize = lp.isFullSpan() ? spanCount : 1;
        orientation = lm.getOrientation();
    }
    boolean isFirstRowOrColumn = isFirstRowOrColumn(position, adapter, spanIndex, itemType);
    boolean isLastRowOrColumn = isLastRowOrColumn(position, adapter, spanIndex, spanCount, spanSize, itemType);
    // Reset offset values
    int left = 0, top = 0, right = 0, bottom = 0;
    if (orientation == GridLayoutManager.VERTICAL) {
        int index = spanIndex;
        if (withLeftEdge)
            index = spanCount - spanIndex;
        left = deco.left * index / spanCount;
        index = (spanCount - (spanIndex + spanSize - 1) - 1);
        if (withRightEdge)
            index = spanIndex + spanSize;
        right = deco.right * index / spanCount;
        if (isFirstRowOrColumn && (withTopEdge)) {
            top = deco.top;
        }
        if (isLastRowOrColumn) {
            if (withBottomEdge) {
                bottom = deco.bottom;
            }
        } else {
            bottom = deco.bottom;
        }
    } else {
        int index = spanIndex;
        if (withTopEdge)
            index = spanCount - spanIndex;
        top = deco.top * index / spanCount;
        index = (spanCount - (spanIndex + spanSize - 1) - 1);
        if (withBottomEdge)
            index = spanIndex + spanSize;
        bottom = deco.bottom * index / spanCount;
        if (isFirstRowOrColumn && (withLeftEdge)) {
            left = deco.left;
        }
        if (isLastRowOrColumn) {
            if (withRightEdge) {
                right = deco.right;
            }
        } else {
            right = deco.right;
        }
    }
    outRect.set(left, top, right, bottom);
    applySectionGap(outRect, adapter, position, orientation);
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) RecyclerView(androidx.recyclerview.widget.RecyclerView) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) SuppressLint(android.annotation.SuppressLint)

Example 9 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project FlexibleAdapter by davideas.

the class FlexibleLayoutManager method findLastCompletelyVisibleItemPosition.

/**
 * Helper method to find the adapter position of the <b>last completely</b> visible view
 * [for each span], no matter which Layout is.
 *
 * @return the adapter position of the <b>last fully</b> visible item or {@code RecyclerView.NO_POSITION}
 * if there aren't any visible items.
 * @see #findLastVisibleItemPosition()
 * @since 5.0.0-b8
 */
@Override
public int findLastCompletelyVisibleItemPosition() {
    RecyclerView.LayoutManager layoutManager = getLayoutManager();
    if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGLM = (StaggeredGridLayoutManager) layoutManager;
        int position = staggeredGLM.findLastCompletelyVisibleItemPositions(null)[0];
        for (int i = 1; i < getSpanCount(); i++) {
            int nextPosition = staggeredGLM.findLastCompletelyVisibleItemPositions(null)[i];
            if (nextPosition > position) {
                position = nextPosition;
            }
        }
        return position;
    } else {
        return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
    }
}
Also used : RecyclerView(androidx.recyclerview.widget.RecyclerView) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 10 with StaggeredGridLayoutManager

use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project FlexibleAdapter by davideas.

the class OverallAdapter method showLayoutInfo.

/*
     * HEADER/FOOTER VIEW
     * This method show how to add Header/Footer View as it was for ListView.
     * The secret is the position! 0 for Header; itemCount for Footer ;-)
     * The view is represented by a custom Item type to better represent any dynamic content.
     */
public void showLayoutInfo(boolean scrollToPosition) {
    if (!hasFilter()) {
        // Define Example View
        final ScrollableLayoutItem item = new ScrollableLayoutItem("LAY-L");
        if (mRecyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
            item.setId("LAY-S");
            item.setTitle(mRecyclerView.getContext().getString(R.string.staggered_layout));
        } else if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
            item.setId("LAY-G");
            item.setTitle(mRecyclerView.getContext().getString(R.string.grid_layout));
        } else {
            item.setTitle(mRecyclerView.getContext().getString(R.string.linear_layout));
        }
        item.setSubtitle(mRecyclerView.getContext().getString(R.string.columns, String.valueOf(getFlexibleLayoutManager().getSpanCount())));
        addScrollableHeaderWithDelay(item, 300L, scrollToPosition);
        removeScrollableHeaderWithDelay(item, 2000L);
    }
}
Also used : GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) ScrollableLayoutItem(eu.davidea.samples.flexibleadapter.items.ScrollableLayoutItem) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager)

Aggregations

StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)36 RecyclerView (androidx.recyclerview.widget.RecyclerView)25 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)16 View (android.view.View)14 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)9 Toolbar (androidx.appcompat.widget.Toolbar)6 AbsListView (android.widget.AbsListView)5 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)4 WebView (android.webkit.WebView)3 ScrollView (android.widget.ScrollView)3 BindView (butterknife.BindView)3 OnItemLongClickListener (xyz.zpayh.adapter.OnItemLongClickListener)3 DialogInterface (android.content.DialogInterface)2 Adapter (android.widget.Adapter)2 FrameLayout (android.widget.FrameLayout)2 TextView (android.widget.TextView)2 Nullable (androidx.annotation.Nullable)2 ContentLoadingProgressBar (androidx.core.widget.ContentLoadingProgressBar)2 SimpleItemAnimator (androidx.recyclerview.widget.SimpleItemAnimator)2 FloatingActionButton (com.google.android.material.floatingactionbutton.FloatingActionButton)2