Slava Slava - 2 years ago 309
Java Question

Why RecyclerView onBindViewHolder called just once?

Have a strange bug in my code. I use Recycler View (used in the past without any problem). I created test ArrayList for showing it in Recycler but I get the only first row in RecyclerView and after it, the app stops (not crashes) like ArrayList was finished.

My Main Code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_tab, container, false);

Activity activity = getActivity();

//TODO: For testing ONLY
ArrayList<Order> orders = new ArrayList<>();
for(int i = 0; i < 10; i++) {
orders.add(new Order(i, i, i));

// Initialising Orders Recycler View.
OrderAdapter orderAdapter = new OrderAdapter(activity, orders);
RecyclerView recyclerViewOrders = (RecyclerView) view.findViewById(;
recyclerViewOrders.setLayoutManager(new LinearLayoutManager(activity));

return view;


package com.slavafleer.tipcalculator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

* Order Adapter for Order Recycler View
public class OrderAdapter extends RecyclerView.Adapter<OrderHolder> {

private Context context;
private ArrayList<Order> orders;

public OrderAdapter(Context context, ArrayList<Order> orders) {

this.context = context;
this.orders = orders;

public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = (LayoutInflater)

View view = layoutInflater.inflate(R.layout.item_order, parent, false);

return new OrderHolder(view);

// TODO: why it has been done just once ?
public void onBindViewHolder(OrderHolder holder, int position) {

Order order = orders.get(position);


public int getItemCount() {
return orders.size();


package com.slavafleer.tipcalculator;

import android.view.View;
import android.widget.TextView;

* Order Holder for Order Recycler View
public class OrderHolder extends RecyclerView.ViewHolder {

private TextView textViewOrderId;
private TextView textViewDinerId;
private TextView textViewPrice;

public OrderHolder(View itemView) {

textViewOrderId = (TextView) itemView.findViewById(;
textViewDinerId = (TextView) itemView.findViewById(;
textViewPrice = (TextView) itemView.findViewById(;

// Bind Data Object to the Views.
public void bindOrder(Order order) {

textViewOrderId.setText(order.getOrderId() + "");
textViewDinerId.setText(order.getDinerId() + "");
textViewPrice.setText(order.getPrice() + "");


Answer Source

At first, Febi Mathew, thank you for trying to help me with this.

I found how to fix it.

My original line in Gradle was

compile ''

after I did sample just for Recycler and it was the same, I tried an older version

compile ''

And it worked perfectly. So I suppose it is not my bug =). If you know how to report for this bug, you welcome to do it.

Update: What was wrong is I didn't set layout-height for my item as wrap content. And from reason that I don't understand on old version it did not matter but on a new one it just takes all my screen and I didn't think to try to slide it down.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download