Add usage of BottomSheetBehavior with Fragment

Bug: 26507828
Change-Id: I62bb8db4bb6b1c7b8a908ecc88d603347839301a
This commit is contained in:
Yuichi Araki
2016-01-25 15:22:42 +09:00
parent 5e9ef02951
commit 03349d5713
5 changed files with 216 additions and 0 deletions

View File

@@ -253,6 +253,15 @@
</intent-filter>
</activity>
<activity android:name=".widget.BottomSheetWithFragment"
android:label="@string/design_bottomsheet_with_fragment"
android:theme="@style/Theme.Design">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.example.android.support.design.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".widget.BottomSheetModal"
android:label="@string/design_bottomsheet_modal"
android:theme="@style/Theme.BottomSheetModal">

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bottomsheet_with_fragment"
android:padding="16dp"
style="@style/TextAppearance.AppCompat.Headline"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/bottom_sheet_peek_height"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bottomsheet_with_fragment"
android:padding="16dp"
style="@style/TextAppearance.AppCompat.Headline"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<fragment
android:id="@+id/bottom_sheet"
class="com.example.android.support.design.widget.BottomSheetWithFragment$BottomSheetFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"/>
</android.support.design.widget.CoordinatorLayout>

View File

@@ -26,6 +26,7 @@
<string name="design_bottomsheet_persistent">BottomSheet/Persistent</string>
<string name="design_bottomsheet_hideable">BottomSheet/Hideable</string>
<string name="design_bottomsheet_scroll">BottomSheet/With ScrollView in background</string>
<string name="design_bottomsheet_with_fragment">BottomSheet/With Fragment</string>
<string name="design_bottomsheet_modal">BottomSheet/Modal</string>
<string name="fab_size_normal">Normal size</string>
@@ -94,8 +95,10 @@
<string name="bottomsheet_persistent">Persistent</string>
<string name="bottomsheet_hideable">Hideable</string>
<string name="bottomsheet_scroll">With ScrollView</string>
<string name="bottomsheet_with_fragment">With Fragment</string>
<string name="bottomsheet_modal">Modal</string>
<string name="bottomsheet_hide">Hide</string>
<string name="bottomsheet_show">Show</string>
<string name="item_n">Item %d</string>
</resources>

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.support.design.widget;
import com.example.android.support.design.R;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* This demonstrates basic usage of {@link BottomSheetBehavior} with Fragment.
*/
public class BottomSheetWithFragment extends AppCompatActivity {
private BottomSheetBehavior mBottomSheetBehavior;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.design_bottom_sheet_with_fragment);
setUpRecyclerView((RecyclerView) findViewById(R.id.list1));
mBottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet));
}
@Override
public void onBackPressed() {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
super.onBackPressed();
}
}
private static void setUpRecyclerView(RecyclerView recyclerView) {
Context context = recyclerView.getContext();
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(new DummyAdapter(context, 30));
}
public static class BottomSheetFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.design_bottom_sheet_fragment, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
setUpRecyclerView((RecyclerView) view.findViewById(R.id.list2));
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(android.R.layout.simple_list_item_1, parent, false));
text = (TextView) itemView.findViewById(android.R.id.text1);
}
}
public static class DummyAdapter extends RecyclerView.Adapter<ViewHolder> {
private final Context mContext;
private final int mItemCount;
public DummyAdapter(Context context, int itemCount) {
mContext = context;
mItemCount = itemCount;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(mContext), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(mContext.getString(R.string.item_n, position + 1));
}
@Override
public int getItemCount() {
return mItemCount;
}
}
}