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