Reputation: 39
I'm working with Bottom Sheet and Recyclerview.
My expectation is converting the layout manager of RecyclerView from List to Grid.
However, when I changed the LayoutManager, the bottom sheet doesn't show fullscreen anymore.
Here is the Gif:
https://media.giphy.com/media/26FeSME9RKKfIQIPS/giphy.gif
Main Activity:
private BottomSheetBehavior bottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_up_panel);
ListHorizontalFragment fragment = new ListHorizontalFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.containerFragment, fragment).commit();
View bottomSheet = findViewById(R.id.bottom_sheet);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
final float scale = getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);
bottomSheetBehavior.setPeekHeight(pixels);
fragment.changeLayoutManagerToHorizontal();
}
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
fragment.changeLayoutManagerToGrid();
bottomSheet.setMinimumHeight(height);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
Fragment to display items:
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private ArrayList<Integer> colors = new ArrayList<>();
public ListHorizontalFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list_horizontal, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.horizontalRecyclerView);
List<Integer> lstColor = new ArrayList<>();
lstColor.add(R.color.actionbar_blue);
lstColor.add(R.color.black);
lstColor.add(R.color.dark_blue);
lstColor.add(R.color.common_action_bar_splitter);
lstColor.add(R.color.red);
lstColor.add(R.color.yellow);
lstColor.add(R.color.green);
lstColor.add(R.color.pitch_blue);
colors.addAll(lstColor);
changeLayoutManagerToHorizontal();
return view;
}
public void changeLayoutManagerToHorizontal() {
recyclerView.setAdapter(null);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new RecyclerViewAdapter(colors, getActivity());
recyclerView.setAdapter(adapter);
}
public void changeLayoutManagerToGrid() {
recyclerView.setAdapter(null);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
adapter = new RecyclerViewAdapter(colors, getActivity());
recyclerView.setAdapter(adapter);
}
The layout of bottom sheet:
<RelativeLayout
android:orientation="vertical"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_peekHeight="300dp"
android:background="@android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<FrameLayout
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerFragment"/>
</RelativeLayout>
When I expended the bottom sheet, it supposed to be fullscreen. But somehow it doesn't work like I expected
Upvotes: 1
Views: 929
Reputation: 141
You should make new fragment that extends BottomSheetDialogFragment
and override its methods
@Override
public void setupDialog(final Dialog dialog, int style) {
//initialize your view here
}
and call your action from this callback
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
//your code
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//your code
}
};
then from your activity initialize this Bottom sheet fragment.
for more detail check this tutorial http://www.truiton.com/2016/07/android-bottom-sheet-example/ Hope this help.
Upvotes: 1