1/*
2 * Copyright (C) 2016 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 android.content.Context;
20import android.os.Bundle;
21import android.support.annotation.Nullable;
22import android.support.design.widget.BottomSheetDialogFragment;
23import android.support.v7.app.AppCompatActivity;
24import android.support.v7.widget.LinearLayoutManager;
25import android.support.v7.widget.RecyclerView;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29
30import com.example.android.support.design.Cheeses;
31import com.example.android.support.design.R;
32
33abstract class BottomSheetModalBase extends AppCompatActivity {
34
35    private static final String FRAGMENT_MODAL = "modal";
36
37    @Override
38    protected void onCreate(@Nullable Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40        setContentView(R.layout.design_bottom_sheet_modal);
41        findViewById(R.id.show_short).setOnClickListener(mOnClickListener);
42        findViewById(R.id.show_long).setOnClickListener(mOnClickListener);
43    }
44
45    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
46        @Override
47        public void onClick(View v) {
48            switch (v.getId()) {
49                case R.id.show_short:
50                    ModalFragment.newInstance(5)
51                            .show(getSupportFragmentManager(), FRAGMENT_MODAL);
52                    break;
53                case R.id.show_long:
54                    ModalFragment.newInstance(ModalFragment.LENGTH_ALL)
55                            .show(getSupportFragmentManager(), FRAGMENT_MODAL);
56                    break;
57            }
58        }
59    };
60
61    /**
62     * This is the bottom sheet.
63     */
64    public static class ModalFragment extends BottomSheetDialogFragment {
65
66        private static final String ARG_LENGTH = "length";
67
68        public static final int LENGTH_ALL = Cheeses.sCheeseStrings.length;
69
70        public static ModalFragment newInstance(int length) {
71            ModalFragment fragment = new ModalFragment();
72            Bundle args = new Bundle();
73            args.putInt(ARG_LENGTH, Math.min(LENGTH_ALL, length));
74            fragment.setArguments(args);
75            return fragment;
76        }
77
78        @Nullable
79        @Override
80        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
81                @Nullable Bundle savedInstanceState) {
82            return inflater.inflate(R.layout.design_bottom_sheet_recyclerview, container, false);
83        }
84
85        @Override
86        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
87            // For the scrolling content, you can use RecyclerView, NestedScrollView or any other
88            // View that inherits NestedScrollingChild
89            RecyclerView recyclerView =
90                    (RecyclerView) view.findViewById(R.id.bottom_sheet_recyclerview);
91            Context context = recyclerView.getContext();
92            recyclerView.setLayoutManager(new LinearLayoutManager(context));
93            int length = getArguments().getInt(ARG_LENGTH);
94            String[] array = new String[length];
95            System.arraycopy(Cheeses.sCheeseStrings, 0, array, 0, length);
96            recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(context, array));
97        }
98
99    }
100
101}
102