Reputation: 548
I have a viewpager which have several fragments inside. I want to add a bottom sheet to one of the fragments. Lets call it report_fragment. The layouts for the fragment and the bottom sheet are:
<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 layout
<?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>
bottom sheet layout:
<?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>
The java code for fragment is:
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;
}
Unfortunately, no bottom sheet is shown in the fragment. What i have done wrong?
Upvotes: 0
Views: 1989
Reputation: 2701
in your gradle
implementation 'com.google.android.material:material:1.1.0-alpha09'
style
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Mainactivity.java
public class Main2Activity extends AppCompatActivity {
private ViewPager mPager;
private PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.vpPager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ItemFragment();
default:
return new ItemFragment();
}
}
@Override
public int getCount() {
return 1;
}
}
}
activity_main.xml
<androidx.constraintlayout.widget.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="match_parent"
android:fitsSystemWindows="true"
tools:context=".Main2Activity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ItemFragment.java
public class ItemFragment extends Fragment {
View view;
BottomSheetBehavior behavior;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_item_list, container, false);
LinearLayout bottomSheet = view.findViewById(R.id.botttomsheet);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS));
return view;
}
}
fragment_item_list.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:name="com.andy.faceread.fragment.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".fragment.ItemFragment"
tools:listitem="@layout/fragment_item" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/botttomsheet"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@color/colorAccent"
app:layout_behavior="@string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:textColor="@android:color/white"
android:text="bottomsheet"/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Upvotes: 4