RecyclerView is not showing all items in the list

Viewed 21971

I am using RecyclerView in my app. Every time I open my screen I can see only one item but when I debug it is coming every time to onBindViewHolder method.

Here is my adapter:

@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();
}

and this my RecyclerView:

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

If you are using RecyclerView inside ScrollView then replace ScrollView with NestedScrollView.

Enable android:nestedScrollingEnabled="false" in RecyclerView.

This solved my problem.

in your layout tourist_details_info.xml make the parent height from match_parent to specific height (100dp or something) or use wrap_content as android:layout_height="match_parent"

This is the most common mistake everyone makes while using a recyclerView. Instead of using position in the onBindViewHolder you have to use

holder.getAdapterPosition()

while fetching data from a list. The position that you are using in onBindViewHolder is the position of the item on the screen which will be limited to maximum number of items on your current screen.

@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);
    }
}

Hope this helps.

The problem was on the ScrollingView. It frequently happen when we use RecyclerView inside it.

You can simply change ScrollingView with android.core.widget.NestedScrollView.

Why Error is occur

#1 Issue Recycler view Width and height not proper so

You can set it like this (Runtime)

 @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 issue May be you are using ScrollView then replace with NestedScrollView just like this (set
android:nestedScrollingEnabled="false" if this code not work for You)

<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 Check getItemCount() Values

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

I hope this solve Your Problem

I feel so dumb now, but maybe i can help others:

My problem was that the ORIENTATION was setted to HORIZONTAL because i copy and paste from other place of app, change it to VERTICAL it works nice.

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

I got the same error and the reason was the same as @Acaua described. I was using a HORIZONTAL linear layout also, so be sure to check that.

Maybe it will help someone. I had a horizontal RecyclerView inside another vertical one. I used AsyncListDifferDelegationAdapter from Adapter Delegates library and used an EndlessScrollListener for inner RecyclerView to implement lazy loading. But when I scrolled to the end, layoutManager.itemCount of inner RecyclerView was returning 50, but findLastVisibleItemPosition() - 19. So less data was displayed than it had really been. I solved this with making a new list and setting it to the adapter of inner RecyclerView:

innerAdapter.items = myItems.toList()

toList() creates a new list

I had just wrapped my adapter's onBindViewHolder code with try{ } catch() {} block and it's worked

Example:

 @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());
        }
    }
Related