Skip to content

Commit

Permalink
Use GridView verticalSpacing when calculating the grid height. Fixes r…
Browse files Browse the repository at this point in the history
  • Loading branch information
grennis committed Aug 1, 2016
1 parent b50db7b commit 663f906
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.antonyt.infiniteviewpager;

import android.content.Context;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;

import java.util.ArrayList;

Expand Down Expand Up @@ -41,6 +43,7 @@ public class InfiniteViewPager extends ViewPager {
* Use internally to decide height of the calendar
*/
private int rowHeight = 0;
private int rowSpacing = 0;

// ******* Setter and getters *********
public boolean isEnabled() {
Expand Down Expand Up @@ -130,20 +133,28 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

rowHeight = firstChild.getMeasuredHeight();

// Consider gridview vertical spacing (Available on API >= 16 only)
if (firstChild instanceof GridView && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rowSpacing = ((GridView)firstChild).getVerticalSpacing();
}
}

// Calculate height of the calendar
int calHeight;

// If fit 6 weeks, we need 6 rows
if (sixWeeksInCalendar) {
calHeight = rowHeight * 6;
calHeight = (rowHeight * 6) + (rowSpacing * 5);
} else { // Otherwise we return correct number of rows
calHeight = rowHeight * rows;
calHeight = (rowHeight * rows) + (rowSpacing * (rows-1));
}

// Prevent small vertical scroll (if we couldnt find vertical spacing)
if (rowSpacing == 0) {
calHeight -= 12;
}

// Prevent small vertical scroll
calHeight -= 12;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(calHeight,
MeasureSpec.EXACTLY);

Expand Down

0 comments on commit 663f906

Please sign in to comment.