How to detect if RecyclerView is empty?

后端 未结 4 1439
闹比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...

    0 讨论(0)
  • 2021-01-11 10:38

    if (adapter.getItemCount() == 0)

    doing this worked for me...

    0 讨论(0)
  • 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); 
        }
    });
    
    0 讨论(0)
  • 2021-01-11 10:49

    You can check if it's empty by running:

    if (adapter.getItemCount() == 0)
    

    If it's not working it means you haven't Override the getItemCount on your adapter! so make sure it's overrided:

    @Override
    public int getItemCount() {
        return mDataSet.size(); // Where mDataSet is the list of your items
    }
    

    Update: So based on your update this is how you could proceed. In my opinion you just need a callback. You are checking if the list is empty on your onViewCreated. You should, instead, use a callback. Do something like that:

    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.supportsPredictiveItemAnimations();
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setClickable(true);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
    
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Retrieving data from Server");
        pDialog.show();   
    }
    

    In the class you are using to populate your jsonList, I assume an asynctask or a separate class add this:

    private OnJsonLoaded mListener;
    
    public void setOnJsonLoadedListener(OnJsonLoaded listener) {
        mListener = listener;
    }
    
    public interface OnJsonLoaded {
        void onJsonLoaded(ArrayList<HashMap<String, String>> list);
    }
    

    now, in the asynctask that populate ur jsonLise or when the json parser finish his job, call the listener:

    if (mListener != null) {
        mListener.onJsonLoaded(jsonList);
    }
    

    In your fragment (the one with NovaAdapter novaAdapter = new NovaAdapter(getActivity(),jsonList); and your recyclerview) add the interface implementation:

    classThatParseJson.setOnJsonLoadedListener(new OnJsonLoaded() {
            @Override
            public void onJsonLoaded(ArrayList<HashMap<String, String>> list) {
                if (list.size() != 0) {
                    NovaAdapter novaAdapter = new NovaAdapter(getActivity(),jsonList); 
                    recyclerView.setAdapter(novaAdapter);
                } else {
                    // Show something like a dialog that the json list is 0 or do whatever you want... here the jsonlist have a count of 0 so it's empty!
                }  
            }
    });
    

    the code may containts errors, i written it by hand without using IDE so maybe you have to fix small things but the logic is quite clear!

    Update based on your Update 2:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_subnets, container, false);
        recyclerView = (RecyclerView)rootView.findViewById(R.id.subnetsRV);
        return rootView;
    }
    
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.supportsPredictiveItemAnimations();
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setClickable(true);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
    
        // start json parser here instead of passing to fragment as a bundle
        SubnetsParser.parseJSON(yourparams);
    }
    
    @Override
    public void onJsonLoaded(ArrayList<HashMap<String, String>> list) {
    
        SubnetsParser.setOnJSONLoadedListener(new OnJSONLoaded() {
            @Override
            public void onJsonLoaded(ArrayList<HashMap<String, String>> list) {
                if (list.size() != 0){
                    SubnetsAdapter subnetsAdapter = new SubnetsAdapter(getActivity(),jsonList);
                    recyclerView.setAdapter(subnetsAdapter);
                }else {
                    //pDialog = new ProgressDialog(getActivity());
                    //pDialog.setMessage("Retrieving data from Server");
                    //pDialog.show();
                    //Instead of a progressdialog, put here a dialog informing that the list is empty!
                }
            }
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题