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