Recyclerview not call onCreateViewHolder

前端 未结 30 1367
离开以前
离开以前 2020-11-28 07:04

My RecyclerView does not call onCreateViewHolder, onBindViewHolder even MenuViewHolder constructor, therefore nothing app

相关标签:
30条回答
  • 2020-11-28 07:43

    This does not apply for your particular case. But this might help someone else.

    This reason could be careless usage of the following method

    recyclerView.setHasFixedSize(true);
    

    If not used with caution, this might cause the onBindView and onCreateViewHolder to not be called at all, with no error in the logs.

    0 讨论(0)
  • 2020-11-28 07:43

    My encouter was the onBindViewHolder( holder , position) overrided method of the RecyclerView.adaptor not being called. Method onCreateViewHolder was called, getItemCount was called. If you read the specs for Recyclerview Adapter, you will learn that if you have overrided onBindViewHolder( holder, position, List payloads) will be called in favour. So please be careful.

    0 讨论(0)
  • 2020-11-28 07:44

    i forgot to add below line after i adding it worked for me

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    
    0 讨论(0)
  • 2020-11-28 07:44

    Add this to Your Adapter Class

     @Override
        public int getItemCount() {
            return mDataset.size();
        } 
    
    0 讨论(0)
  • 2020-11-28 07:45

    See my answer if you are using android data binding library - Make sure you are setting layout for recyclerview and item count must be greater than 0

     @BindingAdapter({"entries", "layout"})
        public static void setEntries(RecyclerView view, ArrayList<LoginResponse.User> listOfUsers, int layoutId) {
            if (view.getAdapter() == null) {
                view.setLayoutManager(new LinearLayoutManager(view.getContext()));
                SingleLayoutAdapter adapter = new SingleLayoutAdapter(layoutId) {
                    @Override
                    protected Object getObjForPosition(int position) {
    
                        return listOfUsers.get(position);
                    }
    
                    @Override
                    public int getItemCount() {
                        return listOfUsers.size();
                    }
                };
                view.setAdapter(adapter);
            }
        }
    

    Happy coding :-)

    0 讨论(0)
  • 2020-11-28 07:46

    For what it's worth, I observed it when I set the recycler view adapter before the adapter was actually initialized. Solution was to make sure recyclerView.setAdapter(adapter) was called with a non-null adapter

    0 讨论(0)
提交回复
热议问题