How to detect if RecyclerView is empty?

后端 未结 4 1478
闹比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:42

    You can do it using interface callback:

    1. Create interface
    public interface OnAdapterCountListener {
        void onAdapterCountListener(int count);
    }
    
    1. Add below variables and methods in adapter
    private OnAdapterCountListener onAdapterCountListener; 
        public void setOnAdapterCountListener(OnAdapterCountListener l) { 
            onAdapterCountListener = l; 
        }
    
    1. Add this line in onCreateViewHolder of your adapter
    onAdapterCountListener.onAdapterCountListener(getItemCount());
    
    1. Finally, call interface in your activity
    listAdapter.setOnAdapterCountListener(new OnAdapterCountListener() {
        @Override 
        public void onAdapterCountListener(int count) { 
            if (count > 0) 
                adapterEmptyText.setVisibility(View.GONE); 
        }
    });
    

提交回复
热议问题