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 17package com.example.android.support.design.widget; 18 19import com.example.android.support.design.R; 20 21import android.os.Bundle; 22import android.support.annotation.NonNull; 23import android.support.annotation.Nullable; 24import android.support.design.widget.BottomSheetBehavior; 25import android.view.View; 26import android.widget.Button; 27import android.widget.TextView; 28 29 30/** 31 * This demonstrates basic usage of hideable {@link BottomSheetBehavior}. 32 */ 33public class BottomSheetHideable extends BottomSheetUsageBase { 34 35 private TextView mTextSlideOffset; 36 37 private Button mToggle; 38 39 @Override 40 protected int getLayoutId() { 41 return R.layout.design_bottom_sheet_hideable; 42 } 43 44 @Override 45 protected void onCreate(@Nullable Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 mTextSlideOffset = findViewById(R.id.slide_offset); 48 mToggle = findViewById(R.id.toggle); 49 mToggle.setOnClickListener(mOnClickListener); 50 mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 51 @Override 52 public void onStateChanged(@NonNull View bottomSheet, 53 @BottomSheetBehavior.State int newState) { 54 switch (newState) { 55 case BottomSheetBehavior.STATE_HIDDEN: 56 mToggle.setText(R.string.bottomsheet_show); 57 mToggle.setEnabled(true); 58 break; 59 case BottomSheetBehavior.STATE_EXPANDED: 60 case BottomSheetBehavior.STATE_COLLAPSED: 61 mToggle.setText(R.string.bottomsheet_hide); 62 mToggle.setEnabled(true); 63 break; 64 } 65 } 66 67 @Override 68 public void onSlide(@NonNull View bottomSheet, float slideOffset) { 69 mTextSlideOffset.setText(String.valueOf(slideOffset)); 70 } 71 }); 72 } 73 74 private View.OnClickListener mOnClickListener = new View.OnClickListener() { 75 @Override 76 public void onClick(View v) { 77 if (v.getId() == R.id.toggle && mBehavior != null) { 78 mToggle.setEnabled(false); 79 if (mBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { 80 mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 81 } else { 82 mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); 83 } 84 } 85 } 86 }; 87 88} 89