SwipeStack
SwipeStack copied to clipboard
How to vertically center SwipeStack?
I can't seem to vertically center the SwipeStack inside any ViewGroup. I've tried inside a FrameLayout, RelativeLayout and LinearLayout.
Is it possible? If not, any hints on how I can modify the SwipeStack implementation to achieve this?
@kaciula I believe you can do it if you modify method implementation reorderItems in SwipeStack class, this method is used to adjust padding and spacing for the swipe deck stacks.
I noticed that the SwipeStack always reports the full height of the parent ViewGroup so I tried to compute the actual size and set it in onMeasure. Still, I can't manage to get the height right as the cards are being cut off at the bottom. Each card shows an image fetched from the Internet plus some texts and buttons.
`@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int containerWidth = MeasureSpec.getSize(widthMeasureSpec);
int maxChildHeight = 0;
final int childCount = getChildCount();
int topViewIndex = childCount - 1;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
int childHeight = child.getMeasuredHeight();
if (childHeight > maxChildHeight) {
maxChildHeight = childHeight;
}
}
int heightOffset = 0;
for (int x = 0; x < childCount; x++) {
int distanceToViewAbove = (topViewIndex * mViewSpacing) - (x * mViewSpacing);
heightOffset += distanceToViewAbove;
}
setMeasuredDimension(containerWidth, maxChildHeight + heightOffset);
}`
Here is how to get the entire card stack or (top view on the stack) centered vertically:
In the SwipeStack.reorderItems() method, change newPositionY calculation as show below:
// int newPositionY = distanceToViewAbove + getPaddingTop(); <--- ORIGINAL CALCULATION
// If you want to center the entire stack vertically use the following:
int newPositionY = (getHeight() - childView.getMeasuredHeight() - (topViewIndex * mViewSpacing)) / 2
+ distanceToViewAbove;
// If you want to center the top view on the stack vertically use the following:
int newPositionY = (getHeight() - childView.getMeasuredHeight()) / 2
+ distanceToViewAbove;