Why is the RecyclerView onBindViewHolder called only once?

I have a strange error in my code. I am using Recycler View (used in the past without any problems). I created an ArrayList test to show it in Recycler, but I got the only first line in the RecyclerView and after it the application stops (does not work), as ArrayList is completed.

My main code is:

@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() + ""); } } 
+7
java android android-recyclerview recycler-adapter
source share
5 answers

First, Feb-Matthew, thanks 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 made a selection only 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. Therefore, I believe that this is not my mistake =). If you know how to report this error, you can do it.

Update: Something is wrong: I did not set the layout height for my element as the contents of the wrapper. And because of the mind, which I don’t understand on the old version, it didn’t matter, but on the new one, it just takes my whole screen, and I didn’t think to try to shift it.

+7
source share

I also ran into the same problem because the wrong height parameter was set.

It should be wrap_content , but I set layout-height = match_parent.

Thus, the first list item will occupy all the space in the list view, so it only displays the list item.

+3
source share

Check this if the outer element of the element layout height is set to "wrap_content". Example:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout mlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" // Just like this! > <TextView android:id="@+id/item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
+1
source share

Can you try this?

 @Override public RecyclerView.ViewHolder 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); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Order order = orders.get(position); OrderHolder orderHolder = (OrderHolder)holder; orderHolder.bindOrder(order); } 
0
source share

My Previous commit Recyclerview support version: 24.2.1

I later updated build.gradle by adding the following:

 > configurations.all { > resolutionStrategy.eachDependency { DependencyResolveDetails details -> > def requested = details.requested > if (requested.group == 'com.android.support') { > if (!requested.name.startsWith("multidex")) { > details.useVersion '25.3.0' > } > } > } } 

So, after that all versions of support were upgraded to 25.3.0, with the exception of "multidex". This means that the recyclerview version is also updated.

Here is the problem, after that the My recyclerview onbindviewholder method is launched, which is not called.

As I found this reason after commenting above code and running, I can use my recyclerview

Hope this can help someone.

Relations Vinod

0
source share

All Articles