Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bottomsheet TextView height not adjusted on first time expanding

TL;DR: TextView in Bottomsheet not showing wrapped multi-line text the first time Bottomsheet is expanded, but adjust itself after it collapsed.

So I am using the Bottomsheet from design-23.2.1 library.

My layout file looks like this:

<android.support.design.widget.CoordinatorLayout>

    ......

    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
</android.support.design.widget.CoordinatorLayout>

The content of the Bottomsheet is basically a list:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:singleLine="false" />
    ...
</LinearLayout>

The issue is whenever the Bottomsheet is set to STATE_EXPANDED the first time, the TextView is single line and text is wrapped, and there is no ellipsis at line end.

Then after it's set to STATE_COLLAPSED, TextView's height is fine and multi-lined properly.

I know height re-layout happened after set to STATE_COLLAPSED because I slide it from collapse and the multi-line is already there.

A work around is provided here. I followed it and added this:

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_EXPANDED) {
            bottomSheetBehavior.onLayoutChild(coordinatorLayout,
                                            bottomSheetView,
                                            ViewCompat.LAYOUT_DIRECTION_LTR);
        }
    }
    ........
}

It did actually make the height re-adjust when Bottomsheet is expanded the first time. However it occurred abruptly right after the expanding animation is done.

Is there any way to adjust the height ahead of the expanding animation just like how Google Map does?

Update

I found that this issue is because I have set Bottomsheet to STATE_COLLAPSED before it was expanded. If that was not set then the problem is gone and height is adjusted properly the first time.

Now my question is: why set it to STATE_COLLAPSED before expanding will cause that issue?

like image 517
Peike Avatar asked Mar 27 '16 16:03

Peike


1 Answers

if for some reason you still have to use old support library, here is the workaround for this.

mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull final View bottomSheet, int newState) {
            bottomSheet.post(new Runnable() {
                @Override
                public void run() {
                    //workaround for the bottomsheet  bug
                    bottomSheet.requestLayout();
                    bottomSheet.invalidate();
                }
            });
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    });
like image 74
Ankush Chugh Avatar answered Nov 10 '22 16:11

Ankush Chugh