Slava Slava - 2 months ago 15
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:

@Override
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(R.id.recylerViewAllDiners);
recyclerViewOrders.setLayoutManager(new LinearLayoutManager(activity));
recyclerViewOrders.setAdapter(orderAdapter);

return view;
}


Adapter:

package com.slavafleer.tipcalculator;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
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;
}

@Override
public OrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {

LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

return new OrderHolder(view);
}

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

Order order = orders.get(position);

holder.bindOrder(order);
}

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


Holder:

package com.slavafleer.tipcalculator;

import android.support.v7.widget.RecyclerView;
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) {
super(itemView);

textViewOrderId = (TextView) itemView.findViewById(R.id.textViewOrderId);
textViewDinerId = (TextView) itemView.findViewById(R.id.textViewDinerId);
textViewPrice = (TextView) itemView.findViewById(R.id.textViewPrice);
}

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

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


}

Answer

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 'com.android.support:recyclerview-v7:23.2.0'

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

compile 'com.android.support:recyclerview-v7:23.1.1'

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.