Search in sources :

Example 1 with SpanSizeLookup

use of android.support.v7.widget.GridLayoutManager.SpanSizeLookup in project MultiType by drakeet.

the class BilibiliActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    adapter = new MultiTypeAdapter();
    adapter.applyGlobalMultiTypePool();
    adapter.register(Post.class, new PostViewBinder());
    adapter.register(PostList.class, new HorizontalPostsViewBinder());
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT);
    SpanSizeLookup spanSizeLookup = new SpanSizeLookup() {

        @Override
        public int getSpanSize(int position) {
            Object item = items.get(position);
            return (item instanceof PostList || item instanceof Category) ? SPAN_COUNT : 1;
        }
    };
    layoutManager.setSpanSizeLookup(spanSizeLookup);
    recyclerView.setLayoutManager(layoutManager);
    int space = getResources().getDimensionPixelSize(R.dimen.normal_space);
    recyclerView.addItemDecoration(new PostItemDecoration(space, spanSizeLookup));
    recyclerView.setAdapter(adapter);
    JsonData data = new JsonData();
    items = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        /* You also could use Category as your CategoryItemContent directly */
        items.add(data.category0);
        items.add(data.postArray[0]);
        items.add(data.postArray[1]);
        items.add(data.postArray[2]);
        items.add(data.postArray[3]);
        items.add(data.postArray[0]);
        items.add(data.postArray[1]);
        items.add(new PostList(data.postList));
    }
    adapter.setItems(items);
    adapter.notifyDataSetChanged();
}
Also used : Category(me.drakeet.multitype.sample.common.Category) SpanSizeLookup(android.support.v7.widget.GridLayoutManager.SpanSizeLookup) GridLayoutManager(android.support.v7.widget.GridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) MultiTypeAdapter(me.drakeet.multitype.MultiTypeAdapter)

Example 2 with SpanSizeLookup

use of android.support.v7.widget.GridLayoutManager.SpanSizeLookup in project epoxy by airbnb.

the class VerticalGridCardSpacingDecoration method getItemOffsets.

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
    if (outerPadding == -1 || innerPadding == -1) {
        DisplayMetrics m = view.getResources().getDisplayMetrics();
        outerPadding = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, OUTER_PADDING_DP, m);
        innerPadding = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, INNER_PADDING_DP, m);
    }
    int position = parent.getChildAdapterPosition(view);
    final GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
    final SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
    // Zero everything out for the common case
    outRect.setEmpty();
    int spanSize = spanSizeLookup.getSpanSize(position);
    int spanCount = layoutManager.getSpanCount();
    int spanIndex = spanSizeLookup.getSpanIndex(position, spanCount);
    if (spanSize == spanCount) {
        // Only item in row
        outRect.left = outerPadding;
        outRect.right = outerPadding;
    } else if (spanIndex == 0) {
        // First item in row
        outRect.left = outerPadding;
        outRect.right = innerPadding;
    } else if (spanIndex == spanCount - 1) {
        // Last item in row
        outRect.left = innerPadding;
        outRect.right = outerPadding;
    } else {
        // Inner item (not relevant for less than three columns)
        outRect.left = innerPadding;
        outRect.right = innerPadding;
    }
}
Also used : SpanSizeLookup(android.support.v7.widget.GridLayoutManager.SpanSizeLookup) GridLayoutManager(android.support.v7.widget.GridLayoutManager) DisplayMetrics(android.util.DisplayMetrics)

Example 3 with SpanSizeLookup

use of android.support.v7.widget.GridLayoutManager.SpanSizeLookup in project baseAdapter by hongyangAndroid.

the class WrapperUtils method onAttachedToRecyclerView.

public static void onAttachedToRecyclerView(RecyclerView.Adapter innerAdapter, RecyclerView recyclerView, final SpanSizeCallback callback) {
    innerAdapter.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
        final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
        final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                return callback.getSpanSize(gridLayoutManager, spanSizeLookup, position);
            }
        });
        gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) StaggeredGridLayoutManager(android.support.v7.widget.StaggeredGridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView)

Example 4 with SpanSizeLookup

use of android.support.v7.widget.GridLayoutManager.SpanSizeLookup in project boilerplate by koush.

the class GridRecyclerView method setNumColumns.

private void setNumColumns(Context context, int numColumns) {
    if (gridLayoutManager == null) {
        gridLayoutManager = new GridLayoutManager(context, numColumns);
        typeToSpan = new Hashtable<Integer, Integer>();
        gridLayoutManager.setSpanSizeLookup(spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                int viewType = getAdapter().getItemViewType(position);
                Integer span = typeToSpan.get(viewType);
                if (span != null)
                    return span;
                ViewHolder vh = getAdapter().createViewHolder(GridRecyclerView.this, viewType);
                int foundSpan;
                if (vh instanceof SpanningViewHolder)
                    foundSpan = ((SpanningViewHolder) vh).getSpanSize(gridLayoutManager.getSpanCount());
                else
                    foundSpan = 1;
                typeToSpan.put(viewType, foundSpan);
                return foundSpan;
            }
        });
        setLayoutManager(gridLayoutManager);
    } else {
        gridLayoutManager.setSpanCount(numColumns);
    }
    typeToSpan.clear();
    spanSizeLookup.invalidateSpanIndexCache();
    requestLayout();
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager)

Example 5 with SpanSizeLookup

use of android.support.v7.widget.GridLayoutManager.SpanSizeLookup in project RecyclerView-FlexibleDivider by yqritc.

the class FlexibleDividerDecoration method getGroupIndex.

/**
     * Returns a group index for GridLayoutManager.
     * for LinearLayoutManager, always returns position.
     *
     * @param position current view position to draw divider
     * @param parent   RecyclerView
     * @return group index of items
     */
private int getGroupIndex(int position, RecyclerView parent) {
    if (parent.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
        GridLayoutManager.SpanSizeLookup spanSizeLookup = layoutManager.getSpanSizeLookup();
        int spanCount = layoutManager.getSpanCount();
        return spanSizeLookup.getSpanGroupIndex(position, spanCount);
    }
    return position;
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) Paint(android.graphics.Paint)

Aggregations

GridLayoutManager (android.support.v7.widget.GridLayoutManager)12 SpanSizeLookup (android.support.v7.widget.GridLayoutManager.SpanSizeLookup)7 RecyclerView (android.support.v7.widget.RecyclerView)7 ActivityManager (android.app.ActivityManager)5 Context (android.content.Context)5 Point (android.graphics.Point)5 Bundle (android.os.Bundle)5 AccessibilityNodeInfoCompat (android.support.v4.view.accessibility.AccessibilityNodeInfoCompat)5 Recycler (android.support.v7.widget.RecyclerView.Recycler)5 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)5 View (android.view.View)5 ImageView (android.widget.ImageView)5 TextView (android.widget.TextView)5 DocumentClipper (com.android.documentsui.DocumentClipper)5 State (com.android.documentsui.State)5 Selection (com.android.documentsui.dirlist.MultiSelectManager.Selection)5 Paint (android.graphics.Paint)3 StaggeredGridLayoutManager (android.support.v7.widget.StaggeredGridLayoutManager)1 DisplayMetrics (android.util.DisplayMetrics)1 MultiTypeAdapter (me.drakeet.multitype.MultiTypeAdapter)1