Skip to content

Instantly share code, notes, and snippets.

@beersheba
Created February 14, 2016 14:22
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save beersheba/cdb69cf600d2d1b9f3c5 to your computer and use it in GitHub Desktop.
public void displayShelf(List<String> images, RecyclerView items) {
//
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
itemWidth = getResources().getDimension(R.dimen.item_width);
padding = (size.x - itemWidth) / 2;
firstItemWidth = getResources().getDimension(R.dimen.padding_item_width);
allPixels = 0;
//
final LinearLayoutManager shopItemslayoutManager = new LinearLayoutManager(this);
shopItemslayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
items.setLayoutManager(shopItemslayoutManager);
//items.setSnapEnabled(true, true); // enable zoom and center snap
items.scrollToPosition(1); // set second element in center
//
items.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
synchronized (this) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
calculatePositionAndScroll(recyclerView);
updateZoom(recyclerView, shopItemslayoutManager);
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
allPixels += dx;
}
});
//
ExtraItemsAdapter adapter = new ExtraItemsAdapter(images.size(), images);
items.setAdapter(adapter);
}
//
private void calculatePositionAndScroll(RecyclerView recyclerView) {
int expectedPosition = Math.round((allPixels + padding - firstItemWidth) / itemWidth);
// Special cases for the padding items
if (expectedPosition == -1) {
expectedPosition = 0;
} else if (expectedPosition >= recyclerView.getAdapter().getItemCount() - 2) {
expectedPosition--;
}
scrollListToPosition(recyclerView, expectedPosition);
}
private void scrollListToPosition(RecyclerView recyclerView, int expectedPosition) {
float targetScrollPos = expectedPosition * itemWidth + firstItemWidth - padding;
float missingPx = targetScrollPos - allPixels;
if (missingPx != 0) {
recyclerView.smoothScrollBy((int) missingPx, 0);
}
}
//
@Override
protected void onResume() {
super.onResume();
final RecyclerView items = (RecyclerView) findViewById(R.id.item_list);
ViewTreeObserver vto = items.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
items.getViewTreeObserver().removeOnGlobalLayoutListener(this);
calculatePositionAndScroll(items);
}
});
}
public void updateZoom(RecyclerView recycler, LinearLayoutManager lm) {
int first = lm.findFirstVisibleItemPosition();
int last = lm.findLastVisibleItemPosition();
for (int i = first; i < last; i++) {
View view = lm.findViewByPosition(i);
int myMidpoint = (view.getRight() - view.getLeft()) / 2;
int recyclerMidpoint = recycler.getWidth() / 2;
float scale = Math.abs(myMidpoint - recyclerMidpoint);
view.setScaleX(scale);
view.setScaleY(scale);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment