onStateChanged() and onSlide() methods are called forever when bottomsheet state changes in Android

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Apr 18, 2017 1:36:29 PM via Website

I asked a question on Stackoverflow however nobody answered it. So i asked the same question here.
Its question topic is the same in the stackoverflow. Since i'm new i couldn't share the link here now.
I copy paste it below :

I developed a bottom sheet with the source codes shown below. However, when i change state of bottom sheet(by changing its state collapsed, expanded, or hide) onStateChanged and onSlide method is called forever. I don't understand why. Is this behaviour normal?

MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private BottomSheetBehavior mBottomSheetBehavior;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View bottomSheet = findViewById( R.id.bottom_sheet );
    Button button1 = (Button) findViewById( R.id.button_1 );
    Button button2 = (Button) findViewById( R.id.button_2 );
    Button button3 = (Button) findViewById( R.id.button_3 );

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

    mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    mBottomSheetBehavior.setHideable(true);
    mBottomSheetBehavior.setPeekHeight(300);
    //mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

    mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(View bottomSheet, int newState) {
            Toast.makeText(getApplicationContext(), "onStateChanged() is called.", Toast.LENGTH_SHORT).show();
            // this method is called forever i dont understand why???
        }

        @Override
        public void onSlide(View bottomSheet, float slideOffset) {
            Toast.makeText(getApplicationContext(), "onStateChanged() is called. in onSlide()", Toast.LENGTH_SHORT).show();
            // this method is also called forever i dont understand why???
        }
    });

}
@Override
public void onClick(View v) {
    switch( v.getId() ) {
        case R.id.button_1: {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            break;
        }
        case R.id.button_2: {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            break;
        }
        case R.id.button_3: {
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            break;
        }
    }
}

}

activity_main.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_green_dark"/>

        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 2"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_blue_light"/>

        <Button
            android:id="@+id/button_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_margin="8dp"
            android:text="Button 3"
            android:textColor="@android:color/white"
            android:background="@android:color/holo_red_dark"/>

    </LinearLayout>

</ScrollView>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:clipToPadding="true"
    android:background="@android:color/holo_orange_light"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/ipsum"
        android:padding="16dp"
        android:textSize="16sp"/>

</android.support.v4.widget.NestedScrollView>

Thanks in advance...

sas

— modified on Apr 18, 2017 1:39:19 PM