【问题标题】:RecyclerView is not showing all items in the listRecyclerView 未显示列表中的所有项目
【发布时间】:2017-11-17 10:52:08
【问题描述】:

我在我的应用程序中使用RecyclerView。每次我打开屏幕时,我只能看到一个项目,但是当我调试时,它每次都会出现在 onBindViewHolder 方法中。

这是我的适配器:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.tourist_details_info, parent, false);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    try {
            holder.displayName.setText(list.get(position).toUpperCase());
    }catch (Exception e){
       AxeltaLogger.err("error>>>"+e);
    }
}
@Override
public int getItemCount() {
    return list.size();
}

这是我的 RecyclerView:

 RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setHasFixedSize(true);
touristAdapter=new TouristInfoAdapter(list);
recyclerView.setAdapter(touristAdapter);

【问题讨论】:

  • 请在此处粘贴您的自定义布局
  • 我认为你的布局 R.layout.tourist_details_info 高度设置为 match_parent
  • 是的,以前是 match_parent,现在它工作正常,感谢 Maicon Hellmann

标签: android android-recyclerview


【解决方案1】:

如果您在ScrollView 中使用RecyclerView,则将ScrollView 替换为NestedScrollView

在 RecyclerView 中启用 android:nestedScrollingEnabled="false"

这解决了我的问题。

【讨论】:

  • 工作 100%,我用 androidx.core.widget.NestedScrollView 替换了我的 ScrollView 并在现在的 NestedScrollView 内的回收站视图中添加了 android:nestedScrollingEnabled="false"。它完全按预期扩展了所有项目!
【解决方案2】:

在您的布局 Tourist_details_info.xml 中,将父级高度从 match_parent 变为特定高度(100dp 或其他)或使用 wrap_content 作为 android:layout_height="match_parent"

【讨论】:

    【解决方案3】:

    这是每个人在使用 recyclerView 时最常犯的错误。而不是在 onBindViewHolder 中使用 position 您必须使用

    holder.getAdapterPosition()
    

    从列表中获取数据时。您在 onBindViewHolder 中使用的 position 是项目在屏幕上的位置,将限制为当前屏幕上的最大项目数。

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        try {
            holder.displayName.setText(list.get(holder.getAdapterPosition().toUpperCase());
        } 
        catch (Exception e) {
            AxeltaLogger.err("error>>>" + e);
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我将 hight 设置为 match_parent 那是它没有显示其他细节
    • 您必须像我在上面的代码中描述的那样更改适配器设置。
    • 在更改高度以包装内容后工作正常
    • 如果项目的数量超过了它们可以容纳的数量,那么您的视图中将会出现重复的项目。检查一下。
    • 没有解决我的问题,问题是我想显示图书信息,有些图书信息已满,有些图书信息很少。我不知道问题出在哪里
    【解决方案4】:

    问题出在 ScrollingView 上。当我们在其中使用 RecyclerView 时,它经常发生。

    您可以简单地使用 android.core.widget.NestedScrollView 更改 ScrollingView。

    【讨论】:

    • 感谢您的回答,但是在这个特定问题的上下文中,我不清楚提问者是否有 ScrollingView;所以在那种情况下,我认为你的答案与问题没有直接关系。
    【解决方案5】:

    为什么会发生错误

    #1 问题 Recycler 视图 Widthheight 不 原来如此

    你可以这样设置(运行时)

     @Override
     public CategoryAdapter.customHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View v = View.inflate(context, R.layout.category_adapter_layout, null);
          RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          v.setLayoutParams(lp);  
          return new CategoryAdapter.customHolder(v);
    }
    

    #2 问题可能是您正在使用 ScrollView 然后替换 使用 NestedScrollView 就像这样(设置
    android:nestedScrollingEnabled="false" 如果这样 代码不适合你)

    <LinearLayout
        android:id="@+id/linear"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
         
                  <androidx.core.widget.NestedScrollView
                     android:id="@+id/nestedTop"
                     android:layout_width="match_parent"
                     android:layout_height="@dimen/_150sdp">
                       
                       <LinearLayout
                        android:layout_width="match_parent"
                        android:orientation="vertical"
                        android:layout_height="wrap_content">
    
                                       
                   <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerCatType"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:padding="10dp"
                    />
             </LinearLayout>
    
        </androidx.core.widget.NestedScrollView>
    

    #3 检查 getItemCount()

        @Override
    public int getItemCount() {
     return accountModelList.size();
    }
    

    我希望这能解决您的问题

    【讨论】:

      【解决方案6】:

      我现在感觉很笨,但也许我可以帮助别人:

      我的问题是 ORIENTATION 设置为 HORIZONTAL 因为我从应用程序的其他位置复制和粘贴,将其更改为 VERTICAL 效果很好。

      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
      linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
      

      【讨论】:

        【解决方案7】:

        我遇到了同样的错误,原因与@Acaua 描述的相同。我也使用了水平线性布局,所以一定要检查一下。

        【讨论】:

        • 有趣的是我也这样做了??
        【解决方案8】:

        也许它会帮助某人。我在另一个垂直的里面有一个水平的RecyclerView。我使用来自Adapter Delegates libraryAsyncListDifferDelegationAdapter 并使用EndlessScrollListener 用于内部RecyclerView 来实现延迟加载。但是当我滚动到最后时,内部RecyclerViewlayoutManager.itemCount 返回50,但findLastVisibleItemPosition() - 19。所以显示的数据比实际少。我通过创建一个新列表并将其设置为内部RecyclerView 的适配器解决了这个问题:

        innerAdapter.items = myItems.toList()
        

        toList() 创建一个新列表

        【讨论】:

          【解决方案9】:

          我刚刚用 try{ } catch() {} 块包装了我的适配器的 onBindViewHolder 代码,它可以工作了

          例子:

           @Override
              public void onBindViewHolder(@NonNull viewholder holder, int position) {
                  PostModel model = postList.get(position);
                  try {
                      Picasso.get()
                              .load(model.getPostImage())
                              .placeholder(R.drawable.sample_cover)
                              .into(holder.binding.postImage);
                      holder.binding.postTime.setText(model.getPostedAt().toString());
                      holder.binding.usernameInPost.setText(model.getPostedBy());
                      Log.i("postsize", "position " + position);
                  } catch (Exception e){
                      Log.i("postsize" , e.toString());
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-09-09
            • 2021-12-20
            • 2018-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多