Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView not displaying data

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 change visibility of other views;
  • I click RecyclerView

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.

like image 502
Marius Kaunietis Avatar asked Nov 09 '22 19:11

Marius Kaunietis


1 Answers

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.

like image 175
Matthew Reilly Avatar answered Nov 14 '22 23:11

Matthew Reilly