ceryle / SegmentedButton

Segmented Control with animation for Android API 12+
Apache License 2.0
364 stars 97 forks source link

SegmentedButton in NestedScrollView not respecting wrap_content height #42

Open addisonElliott opened 5 years ago

addisonElliott commented 5 years ago

Problem

When placing a SegmentedButtonGroup/SegmentedButton within a NestedScrollView, I am getting issues with the height not being set correctly even though the layout_height is set to wrap_content.

Here is an example layout, you should be able to see the issue in the layout inspector.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <co.ceryle.segmentedbutton.SegmentedButtonGroup
            android:id="@+id/buttonGroup_pickupDropoffBoth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:duplicateParentState="false"
            android:fadeScrollbars="false"
            android:filterTouchesWhenObscured="false"
            android:fitsSystemWindows="false"
            android:focusableInTouchMode="false"
            android:hapticFeedbackEnabled="false"
            android:padding="0dp"
            app:sbg_backgroundColor="@color/white"
            app:sbg_position="0"
            app:sbg_radius="2dp"
            app:sbg_selectorColor="@color/colorPrimary87">

            <co.ceryle.segmentedbutton.SegmentedButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:isScrollContainer="false"
                android:padding="4dp"
                android:scrollbars="horizontal"
                app:sb_text="Both"
                app:sb_textColor="#000000"
                app:sb_textColor_onSelection="#FFFFFF" />

            <co.ceryle.segmentedbutton.SegmentedButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="4dp"
                app:sb_text="Pickup"
                app:sb_textColor="#000000"
                app:sb_textColor_onSelection="#FFFFFF" />

            <co.ceryle.segmentedbutton.SegmentedButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="4dp"
                app:sb_text="Dropoff"
                app:sb_textColor="#000000"
                app:sb_textColor_onSelection="#FFFFFF" />

        </co.ceryle.segmentedbutton.SegmentedButtonGroup>

    </androidx.core.widget.NestedScrollView>

</LinearLayout>

Here is a screenshot of what I see in the layout inspector: Screenshot

See that sliver up top, yeah that is it being cut off! Commenting out NestedScrollView makes it work.

Solution

After some digging, I've found the culprit but I'm a bit confused by the source code so I need some assistance.

Regularly, the SegmentedButton will receive an onMeasure with an AT_MOST spec for the height. In this instance, it will return the following. See here for code

textHeight + 2 * paddingTop + 2 * paddingBottom

In a NestedScrollView, instead when sizing it does onMeasure with an UNSPECIFIED and SegmentedButton returns. See here for code

paddingTop + paddingBottom

Questions

@ceryle I am happy to submit a PR if we come to a conclusion for a fix.

Why does the AT_MOST return 2x padding for top/bottom while EXACTLY & UNSPECIFIED do not. This fixes the problem but I don't get the 2x so I was trying to understand.

addisonElliott commented 5 years ago

@ceryle Any update here? This is something I would like to get to the bottom to, please

addisonElliott commented 5 years ago

Decided to recreate this library and introduce changes and upgrades.

My library can be found here, I hope to keep it up-to-date and maintained.

https://github.com/addisonElliott/SegmentedButton