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();
}
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;
}
}
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());
}
}
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();
}
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;
}
Aggregations