我有一个viewpager,里面有几个片段。我想在其中一个片段中添加一个底页。让我们称之为report_fragment。片段和底部工作表的布局为:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<include layout="@layout/report_page_content" />
<!-- include the bottom sheet -->
<iclude layout="@layout/report_bottom_sheet_table" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
report_page_content布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/report_page_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
底部工作表布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/report_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="120dp"
android:background="@color/report_bottom_sheet_bck_color"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:gravity="center"
android:text="@string/daily_bs_tests"
/>
</LinearLayout>
片段的Java代码是:
public class FragmentReportPage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.report_page_content, container, false);
// Init other views ....
// LinearLayout bottomSheetLayout = v.findViewById(R.id.report_bottom_sheet);
// If i uncomment below line, an exception will arise
// BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
// bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
return v;
}
不幸的是,该片段中没有显示底页。我做错了什么?
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());
在你的gradle中