1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.support.design.widget; 18 19 import android.os.Bundle; 20 import android.view.View; 21 import android.widget.Button; 22 import android.widget.TextView; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 27 import com.example.android.support.design.R; 28 import com.google.android.material.bottomsheet.BottomSheetBehavior; 29 30 31 /** 32 * This demonstrates basic usage of hideable {@link BottomSheetBehavior}. 33 */ 34 public class BottomSheetHideable extends BottomSheetUsageBase { 35 36 private TextView mTextSlideOffset; 37 38 private Button mToggle; 39 40 @Override getLayoutId()41 protected int getLayoutId() { 42 return R.layout.design_bottom_sheet_hideable; 43 } 44 45 @Override onCreate(@ullable Bundle savedInstanceState)46 protected void onCreate(@Nullable Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 mTextSlideOffset = findViewById(R.id.slide_offset); 49 mToggle = findViewById(R.id.toggle); 50 mToggle.setOnClickListener(mOnClickListener); 51 mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 52 @Override 53 public void onStateChanged(@NonNull View bottomSheet, 54 @BottomSheetBehavior.State int newState) { 55 switch (newState) { 56 case BottomSheetBehavior.STATE_HIDDEN: 57 mToggle.setText(R.string.bottomsheet_show); 58 mToggle.setEnabled(true); 59 break; 60 case BottomSheetBehavior.STATE_EXPANDED: 61 case BottomSheetBehavior.STATE_COLLAPSED: 62 mToggle.setText(R.string.bottomsheet_hide); 63 mToggle.setEnabled(true); 64 break; 65 } 66 } 67 68 @Override 69 public void onSlide(@NonNull View bottomSheet, float slideOffset) { 70 mTextSlideOffset.setText(String.valueOf(slideOffset)); 71 } 72 }); 73 } 74 75 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 76 @Override 77 public void onClick(View v) { 78 if (v.getId() == R.id.toggle && mBehavior != null) { 79 mToggle.setEnabled(false); 80 if (mBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { 81 mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 82 } else { 83 mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 84 } 85 } 86 } 87 }; 88 89 } 90