Skip to content

Commit

Permalink
Merge pull request #9513 from jquick-axway/TIMOB-25360
Browse files Browse the repository at this point in the history
[TIMOB-25360] Android: ScrollView enhancements
  • Loading branch information
Lokesh Choudhary committed Oct 23, 2017
2 parents 88ca675 + f3fec18 commit e5f3c08
Show file tree
Hide file tree
Showing 5 changed files with 674 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.MeasureSpec;


/**
Expand All @@ -16,6 +18,8 @@
* Extends Google's "SwipeRefreshLayout" class by adding a new setSwipeRefreshEnabled() method.
* Allows the swipe-down feature to be disabled independently of the setEnabled() method that
* Google's implementation uses to disable this feature (along with touch support).
* <p>
* Also adds WRAP_CONTENT support for height. (Google's implementation does not support this.)
*/
public class TiSwipeRefreshLayout extends SwipeRefreshLayout
{
Expand Down Expand Up @@ -61,4 +65,61 @@ public void setSwipeRefreshEnabled(boolean value)
{
this.isSwipeRefreshEnabled = value;
}

/**
* Called when this view's measure() method gets called. Typically called by the parent view.
* Updates this view's width and height based on the given width and height constraints.
* <p>
* Given arguments size and size mode can be extracted by the Android "View.MeasureSpec" class.
* @param widthMeasureSpec Provides the parent's width contraints and size mode.
* @param heightMeasureSpec Provides the parent's height contraints and size mode.
*/
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// If height mode not set to "exactly", then change height to match view's tallest child.
// Note: We need to do this since Google's "SwipeRefreshLayout" class ignores the
// WRAP_CONTENT setting and will fill the height of the parent view instead.
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
// Determine the min height needed to fit this view's tallest child view.
int minHeight = 0;
for (int index = getChildCount() - 1; index >= 0; index--) {
// Fetch the next child.
View child = getChildAt(index);
if (child == null) {
continue;
}

// Skip child views that are flagged as excluded from the layout.
if (child.getVisibility() == View.GONE) {
continue;
}

// Determine the height of the child.
child.measure(widthMeasureSpec, heightMeasureSpec);
int childHeight = child.getMeasuredHeight();
childHeight += child.getPaddingTop() + child.getPaddingBottom();

// Store the child's height if it's the tallest so far.
minHeight = Math.max(minHeight, childHeight);
}

// Make sure we're not exceeding the suggested min height assigned to this view.
minHeight = Math.max(minHeight, getSuggestedMinimumHeight());

// Update this view's given height spec to match the tallest child view, but only if:
// - Height mode is UNSPECIFIED. (View can be any height it wants.)
// - Height mode is AT_MOST and the min height is less than given height.
if ((heightMode == MeasureSpec.UNSPECIFIED) ||
(minHeight < MeasureSpec.getSize(heightMeasureSpec)))
{
heightMode = MeasureSpec.AT_MOST;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(minHeight, heightMode);
}
}

// Update this view's measurements.
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

0 comments on commit e5f3c08

Please sign in to comment.