我希望将Persistent Bottom Sheet作为Fragment嵌套在另一个Fragment中,但是找不到任何实现方法。到处都是将Persistent Bottom Sheet添加为活动/片段内的布局,但是否可能将Persistent Bottom Sheet作为Fragment,以便我可以在其他地方重用该视图?
我希望将Persistent Bottom Sheet作为Fragment嵌套在另一个Fragment中,但是找不到任何实现方法。到处都是将Persistent Bottom Sheet添加为活动/片段内的布局,但是否可能将Persistent Bottom Sheet作为Fragment,以便我可以在其他地方重用该视图?
在AndroidHive上有一份关于BottomSheet
的精彩教程。
你可以创建一个继承BottomSheetDialogFragment
的类。
public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container,
false);
}
}
并使用以下代码切换它
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
BottomSheetBehavior
类。 - Michael LamFragmentContainerView
,然后将你想要重用的Fragment添加到FragmentContainerView
中。您可以使用BottomSheetDialogFragment,我想您想要做的就像这样:
然后创建navigationView文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@drawable/state_pressed"
app:menu="@menu/activity_main_drawer" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
创建文件菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/round_home_24"
android:title="Inicio"
android:checked="true"
android:checkable="true"/>
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/outline_settings_24"
android:title="Configuración"
android:checkable="true"/>
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/round_mail_outline_24"
android:title="Email"
android:checkable="true"/>
<item
android:id="@+id/nav_manage"
android:icon="@drawable/outline_search_24"
android:title="Buscar" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/outline_lock_24"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/outline_account_circle_24"
android:title="Send" />
</menu>
</item>
</menu>
并创建borderRadius文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff0000" />
<corners android:bottomRightRadius="90dp" android:bottomLeftRadius="90dp" />
<padding android:left="30dp"/>
</shape>
</item>
<item android:state_checked="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:bottomRightRadius="30dp" android:topRightRadius="30dp" />
</shape>
</item>
</selector>
并创建按下状态的背景颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/border_radius_tabs">
<color android:color="@color/colorPrimary"></color>
</item>
<item android:state_checked="false">
<color android:color="@color/primaryLightColor"></color>
</item>
</selector>