2

I poulate my RecyclerView with 250 items. Each view only has 4 TextViews. When creating the adapter for the RecyclerView onCreateViewHolder takes about 5 ms and onBindViewHolder takes about 3 ms. For 250 items we then have a delay of 2 seconds. In this two seconds the UI freezes, since the adapter assignment has to be executed on the Main-Thread.

For example in the onCreateViewHolder there is not much going on beside a layout inflation:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    long lTime = System.currentTimeMillis();
    switch (viewType) {
        case TYPE_HEADER:
            SpendingHeader lResult = new SpendingHeader(LayoutInflater.from(mContext).inflate(R.layout.layout_spending_item_header, null));
            LogUtils.logTime(lTime, TAG, "onCreateViewHolder");
            return lResult;
        default:
            SpendingItem lResult2 = new SpendingItem(LayoutInflater.from(mContext).inflate(R.layout.layout_spending_item, null));
            LogUtils.logTime(lTime, TAG, "onCreateViewHolder");
            return lResult2;
    }
}

The complete computation stuff is done on a background thread in an AsyncTask. Only onCerateViewHolder and onBindViewHolder take so much time such that the progress bar freezes. I'm fine to wait but its not good that the progressbar freezes. Is there a way to circumvent this? Also I dont want a lazy list where you get a progressbar for example after scroll to the 30th item.

Note: Also my Recyclerview must be part of a NestedScrollView for actionbarBar ControllableLayout behaviour. Might this be a problem? See here

Community
  • 1
  • 1
softwaresupply
  • 1,908
  • 3
  • 20
  • 34
  • 1
    It looks like the problem is the NestedScrollView. Usually onCreateViewHolder and onBindViewHolder are not called 250 times immediately (except when all of them fit onto the screen). Normally onBindViewHolder is only called when a new View enters the screen. – Damian Jäger Feb 24 '17 at 15:32
  • Recyclerview already implements `NestedScrollingChild` so you most likely do not need to place it in a NestedScrolling Layout to gain AppbarLayout behaviors. – Kuffs Feb 24 '17 at 15:42

1 Answers1

7

You don't need to wrap RecyclerView with NestedScrollView. It will behave correctly with AppBarLayout even without it.

The reason of your lag is that RecyclerView Adapter creates ViewHolders and binds them to all items it has (250 in your case), it happens because NestedScrollView makes RecyclerView measure itself in a wrong way (to match NestedScrollView size), and in this situation RecyclerView "thinks" it needs to create enough ViewHolders to fit to this wrong size.

You can also try to call setAutoMeasureEnabled(true) to make it measure itself properly.

Dimezis
  • 1,541
  • 11
  • 24
  • Yes found out thats exactly the reason. I f you want to have that AppBarLayout at a long RecyclerView at once - your'e done ;) – softwaresupply Feb 28 '17 at 12:48
  • confirmed, without nestedscrollview recyclerview still works with appbarlayout. just ensure to set app:layout_behavior also – j2emanue Dec 17 '22 at 08:19