I have a RecyclerView, which is a BottomSheet, from 23.2 support library:
<android.support.v7.widget.RecyclerView
android:id="@+id/bottom_sheet_multi_car"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_height"
android:background="@color/background"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
Initially, I create adapter with empty dataset:
mMultiCarAdapter = new CarListRecyclerAdapter();
bottomSheetMultiCar.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
bottomSheetMultiCar.setLayoutManager(new LinearLayoutManager(getContext()));
bottomSheetMultiCar.setAdapter(mMultiCarAdapter);
...
public static class CarListRecyclerAdapter extends RecyclerView.Adapter<CarListRecyclerAdapter.ViewHolder> {
private List<Car> mCars = new ArrayList<>();
I have a method to replace my data:
public void replaceItems(final List<Car> cars) {
mCars.clear();
mCars.addAll(cars);
notifyDataSetChanged();
}
I show the RecyclerView:
private void showMultiCar(final List<Car> cars) {
mBottomSheetMultiCarBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mMultiCarAdapter.replaceItems(cars);
}
After I show the RecyclerView, it appears(I see a BottomSheet with RecyclerView's background color). However, no items are visible... until I do some action. Confirmed actions that make RecyclerView show items:
I tried multiple things.
First, changing visibility of RecyclerView after adding items, but it appears that some time must pass for that to be effective.
I tried several methods on it: requestLayout, invalidate.
The recyclerView needs to be a fixed size:
mRecyclerView.setHasFixedSize(true);
Took way too long to figure this out. Not sure why it works, but it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With