Recyclerview not call onCreateViewHolder

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

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

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

    For me, it was because the setHasStableIds(true); method was set. Remove that and it will work

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

    If using a custom adapter do not forget to set the ItemCount to the size of your collection.

    0 讨论(0)
  • 2020-11-28 08:00

    Please set layout manager to RecyclerView like below code:

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_right);
    //set your recycler view adapter here
    NavigationAdapter adapter = new NavigationAdapter(this, FragmentDrawer.getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
    0 讨论(0)
  • 2020-11-28 08:01

    In my case, my list size was 0 and it was not calling the onCreateViewHolder method. I needed to display a message at center for the empty list as a placeholder so I did it like this and it worked like charm. Answer by @Konstantin Burov helped.

      @Override
    public int getItemCount() {
        if (contactList.size() == 0) {
            return 1;
        } else {
            return contactList.size();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 08:02

    Maybe it helps someone but if you set your RecycleView's visibility to GONE the adapter methods won't be called at all... Took me some time to figure it out.

    0 讨论(0)
  • 2020-11-28 08:02

    In my case I set the ID of my RecyclerView to "recyclerView". It seems that this ID was already defined somewhere, because when I changed that to different ID, the methods from the adapter were called properly.

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