Recyclerview not call onCreateViewHolder

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

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

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

    Your getItemCount method returns 0. So RecyclerView never tries to instantiate a view. Make it return something greater than 0.

    for example

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

    Please do the following thing

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

    recyclerView.setAdapter(adapter); called The recyclerView list is populated by calling the method.

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

    Another one is make sure you set layout manager to RecyclerView:

    recycler.setLayoutManager(new LinearLayoutManager(this));
    
    0 讨论(0)
  • 2020-11-28 07:37

    In my case after changing Activity to use ViewModel and DataBinding, I had forgotten to remove setContentView method calling from onCreate method. By removing it my problem solved.

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

    Make sure your XML and Koltin/Java code match. In my case, I had a problem with the ViewHolder since I had written:

    var txt: AppCompatTextView = itemView.findViewById(R.id.txt)
    

    But since my XML code is:

    <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            .../>
    

    I had to change the privious row into:

    var txt: TextView = itemView.findViewById(R.id.txt)
    

    In addition, I also had to remove:

    recyclerView.setHasFixedSize(true);
    

    I hope this can help you :)

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