How to detect if RecyclerView is empty?

后端 未结 4 1481
闹比i
闹比i 2021-01-11 10:12

I have a RecyclerView getting external JSON data parsed from a server. It works fine however the Volley async task on JSON sometimes takes a while and when it does the fragm

4条回答
  •  长情又很酷
    2021-01-11 10:34

    How is described in https://developer.android.com/training/material/lists-cards.html

    The overriden method getItemCount() is invoked by the layout manager. This is the snippet:

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
    

    So to detect if the recyclerView is empty you must request it to your LayoutManager. Example:

    if( mLayoutManager.getItemCount() == 0 ){
        //Do something
    }
    

    I try to getItemCount() of my Adapter but this returns 0, I don't know why it is...

提交回复
热议问题