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 com.example.android.support.design.R; 20 21import android.content.Context; 22import android.os.Bundle; 23import android.support.annotation.Nullable; 24import android.support.design.widget.BottomSheetBehavior; 25import android.support.v4.app.Fragment; 26import android.support.v7.app.AppCompatActivity; 27import android.support.v7.widget.LinearLayoutManager; 28import android.support.v7.widget.RecyclerView; 29import android.view.LayoutInflater; 30import android.view.View; 31import android.view.ViewGroup; 32import android.widget.TextView; 33 34/** 35 * This demonstrates basic usage of {@link BottomSheetBehavior} with Fragment. 36 */ 37public class BottomSheetWithFragment extends AppCompatActivity { 38 39 private BottomSheetBehavior mBottomSheetBehavior; 40 41 @Override 42 protected void onCreate(@Nullable Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.design_bottom_sheet_with_fragment); 45 setUpRecyclerView((RecyclerView) findViewById(R.id.list1)); 46 mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet)); 47 } 48 49 @Override 50 public void onBackPressed() { 51 if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) { 52 mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 53 } else { 54 super.onBackPressed(); 55 } 56 } 57 58 private static void setUpRecyclerView(RecyclerView recyclerView) { 59 Context context = recyclerView.getContext(); 60 recyclerView.setLayoutManager(new LinearLayoutManager(context)); 61 recyclerView.setAdapter(new DummyAdapter(context, 30)); 62 } 63 64 public static class BottomSheetFragment extends Fragment { 65 66 @Nullable 67 @Override 68 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 69 @Nullable Bundle savedInstanceState) { 70 return inflater.inflate(R.layout.design_bottom_sheet_fragment, container, false); 71 } 72 73 @Override 74 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 75 setUpRecyclerView((RecyclerView) view.findViewById(R.id.list2)); 76 } 77 } 78 79 public static class ViewHolder extends RecyclerView.ViewHolder { 80 81 public TextView text; 82 83 public ViewHolder(LayoutInflater inflater, ViewGroup parent) { 84 super(inflater.inflate(android.R.layout.simple_list_item_1, parent, false)); 85 text = (TextView) itemView.findViewById(android.R.id.text1); 86 } 87 } 88 89 public static class DummyAdapter extends RecyclerView.Adapter<ViewHolder> { 90 91 private final Context mContext; 92 93 private final int mItemCount; 94 95 public DummyAdapter(Context context, int itemCount) { 96 mContext = context; 97 mItemCount = itemCount; 98 } 99 100 @Override 101 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 102 return new ViewHolder(LayoutInflater.from(mContext), parent); 103 } 104 105 @Override 106 public void onBindViewHolder(ViewHolder holder, int position) { 107 holder.text.setText(mContext.getString(R.string.item_n, position + 1)); 108 } 109 110 @Override 111 public int getItemCount() { 112 return mItemCount; 113 } 114 } 115 116} 117