BottomSheetBehaviour with NestedScrollView

1.5k views Asked by At

I used Nestedscrollview as Bottomsheetbehaviour but when it changes its state from normal to expanded some of space from toolbar left but when second time state changed from normal to expanded it works fine..

It looks as it leaves space exactly twice of the margin I provide to the sheet in xml. My code looks like this...

MyLayoutFile.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
tools:context=".fragments.StoreLocationDetailsFragment">

<include layout="@layout/toolbar"/>

<FrameLayout
    android:id="@+id/mapHolder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"/>

<include android:id="@+id/nested_scroll_view"
         layout="@layout/store_details_scroll_view"/>

</android.support.design.widget.CoordinatorLayout>

store_details_scroll_view.xml

<android.support.v4.widget.NestedScrollView
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:layout_marginTop="?android:attr/actionBarSize"
android:background="@color/colorLightGrey"
android:clipToPadding="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/tvStoreName"
        style="@style/styleOswaldBold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="Adidas Shoes"
       android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sub_title"
        android:textAlignment="center"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:background="@color/colorDarkGrey"/>
   </LinearLayout>
</android.support.v4.widget.NestedScrollView>

and code in onClick Function in java file looks like

 View bottomSheet = rootLayout.findViewById(R.id.nested_scroll_view);
        behavior = BottomSheetBehavior.from(bottomSheet);

        behavior.setPeekHeight(new ScreenDimensions(getActivity())
                .getHeightWithGivenPercentage(60));
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_DRAGGING:
                        break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        behavior.setPeekHeight(0);
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

Here rootlayout is my Coordinator layout. Can anyone suggest me solution of the above problem. Thanks in advance.

0

There are 0 answers