Skip to content
This repository has been archived by the owner on Dec 1, 2017. It is now read-only.

Fixed bug with height in horizontal mode #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 111 additions & 2 deletions library/src/main/java/org/lucasr/twowayview/TwoWayView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package org.lucasr.twowayview;

import static android.os.Build.VERSION_CODES.HONEYCOMB;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -68,8 +70,6 @@
import android.widget.ListAdapter;
import android.widget.Scroller;

import static android.os.Build.VERSION_CODES.HONEYCOMB;

/*
* Implementation Notes:
*
Expand Down Expand Up @@ -3762,6 +3762,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
}

if (!mIsVertical && heightMode == MeasureSpec.AT_MOST) {
heightSize = measureHeightOfBiggestChild(heightMeasureSpec, heightSize);
}

if (mIsVertical && widthMode == MeasureSpec.AT_MOST) {
widthSize = measureWidthOfBiggestChild(heightMeasureSpec, widthSize);
}

if (!mIsVertical && widthMode == MeasureSpec.AT_MOST) {
widthSize = measureWidthOfChildren(heightMeasureSpec, 0, NO_POSITION, widthSize, -1);
}
Expand Down Expand Up @@ -4654,6 +4662,57 @@ private int measureHeightOfChildren(int widthMeasureSpec, int startPosition, int
return returnedHeight;
}

/**
* Measures the height of each child and returns the height of the largest
* child. The height includes this TwoWayView's top and bottom padding. If
* maxHeight is provided, the returned height will be the smaller of the
* measured height and the provided maxHeight.<br />
* This is used to calculate the height of the TwoWayView in horizontal
* mode.
*
* @param heightMeasureSpec
* The height measure spec to be given to a childs
* {@link View#measure(int, int)}.
* @param maxHeight
* The maximum height that will be returned.
* @return The height of this TwoWayView's largest child (The TwoWayView
* height in horizontal mode).
*/
private int measureHeightOfBiggestChild(int heightMeasureSpec, final int maxHeight) {

int staticHeight = getPaddingTop() + getPaddingBottom();

final ListAdapter adapter = mAdapter;
if (adapter == null) {
return staticHeight;
}

int largestChildHeight = 0;

// mItemCount - 1 since endPosition parameter is inclusive
final RecycleBin recycleBin = mRecycler;
final boolean shouldRecycle = recycleOnMeasure();
final boolean[] isScrap = mIsScrap;

for (int i = 0; i < adapter.getCount(); i++) {
final View child = obtainView(i, isScrap);

measureScrapChild(child, i, heightMeasureSpec);

// Recycle the view before we possibly return from the method
if (shouldRecycle) {
recycleBin.addScrapView(child, -1);
}

int itemHeight = child.getMeasuredHeight();
if (itemHeight > largestChildHeight) {
largestChildHeight = itemHeight + staticHeight;
}
}

return Math.min(largestChildHeight, maxHeight);
}

/**
* Measures the width of the given range of children (inclusive) and
* returns the width with this TwoWayView's padding and item margin widths
Expand Down Expand Up @@ -4745,6 +4804,56 @@ private int measureWidthOfChildren(int heightMeasureSpec, int startPosition, int
return returnedWidth;
}

/**
* Measures the width of each child and returns the width of the largest
* child. The width includes this TwoWayView's left and right padding. If
* maxWidth is provided, the returned width will be the smaller of the
* measured width and the provided maxWidth.<br />
* This is used to calculate the width of the TwoWayView in vertical mode.
*
* @param widthMeasureSpec
* The width measure spec to be given to a childs
* {@link View#measure(int, int)}.
* @param maxWidth
* The maximum width that will be returned.
* @return The width of this TwoWayView's largest child (The TwoWayView
* width in vertical mode).
*/
private int measureWidthOfBiggestChild(int widthMeasureSpec, final int maxWidth) {

int staticWidth = getPaddingLeft() + getPaddingRight();

final ListAdapter adapter = mAdapter;
if (adapter == null) {
return staticWidth;
}

int largestChildWidth = 0;

// mItemCount - 1 since endPosition parameter is inclusive
final RecycleBin recycleBin = mRecycler;
final boolean shouldRecycle = recycleOnMeasure();
final boolean[] isScrap = mIsScrap;

for (int i = 0; i < adapter.getCount(); i++) {
View child = obtainView(i, isScrap);

measureScrapChild(child, i, widthMeasureSpec);

// Recycle the view before we possibly return from the method
if (shouldRecycle) {
recycleBin.addScrapView(child, -1);
}

int childWidth = child.getMeasuredWidth();
if (childWidth > largestChildWidth) {
largestChildWidth = childWidth + staticWidth;
}
}

return Math.min(largestChildWidth, maxWidth);
}

private View makeAndAddView(int position, int offset, boolean flow, boolean selected) {
final int top;
final int left;
Expand Down