From 277beba6f30ee303e38f00b54a5da3b85a4cda97 Mon Sep 17 00:00:00 2001 From: YuutaW <17158086+trumeet@users.noreply.github.com> Date: Sat, 13 Apr 2019 16:07:11 -0700 Subject: fix(library): fix wrap_content code error Signed-off-by: YuutaW <17158086+Trumeet@users.noreply.github.com> --- .../src/main/java/moe/yuuta/flow/FlowFragment.java | 1 + .../java/moe/yuuta/flow/widgets/FlowPager.java | 33 +++++++--------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/library/src/main/java/moe/yuuta/flow/FlowFragment.java b/library/src/main/java/moe/yuuta/flow/FlowFragment.java index 060e1a3..93e722b 100644 --- a/library/src/main/java/moe/yuuta/flow/FlowFragment.java +++ b/library/src/main/java/moe/yuuta/flow/FlowFragment.java @@ -113,6 +113,7 @@ public final class FlowFragment extends Fragment implements IFlowFragment, View. if (savedInstanceState != null) { Log.d(TAG, "onViewCreated - restore " + savedInstanceState.getInt(ARG_CURRENT, -1)); mPager.setCurrentItem(savedInstanceState.getInt(ARG_CURRENT, 0)); + // TODO: updateUI() should be preformed here, but I did not find a appropriate way to retrieve FlowInfo. The temporarily workaround is to notify update when the view re-creates. } if (mUIUpdateScheduled) { updateUI(); diff --git a/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java b/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java index e76f319..052590e 100644 --- a/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java +++ b/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java @@ -1,14 +1,12 @@ package moe.yuuta.flow.widgets; import android.content.Context; -import android.os.Build; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.ViewPager; public class FlowPager extends ViewPager { @@ -20,30 +18,19 @@ public class FlowPager extends ViewPager { super(context, attrs); } - // Thanks to https://stackoverflow.com/a/32488566/6792243 + // Thanks to https://stackoverflow.com/a/20784791/6792243 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - - if(null != getAdapter()) { - int height = 0; - View child = ((FragmentStatePagerAdapter) getAdapter()).getItem(getCurrentItem()).getView(); - if (child != null) { - child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); - height = child.getMeasuredHeight(); - // TODO: Support api-14 and api-15? - if (Build.VERSION.SDK_INT >= 16 && height < getMinimumHeight()) { - height = getMinimumHeight(); - } - } - - int newHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); - if (getLayoutParams().height != 0 && heightMeasureSpec != newHeight) { - getLayoutParams().height = height; + int height = 0; + for(int i = 0; i < getChildCount(); i++) { + View child = getChildAt(i); + child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); + int h = child.getMeasuredHeight(); + if(h > height) height = h; + } - } else { - heightMeasureSpec = newHeight; - } + if (height != 0) { + heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); -- cgit v1.2.3