我的代码中有一个奇怪的错误。我使用了Recycler View(过去使用时没有任何问题)。我创建了测试ArrayList以便在Recycler中显示它,但是在RecyclerView中只有第一行,之后,应用程序停止(不会崩溃),就像ArrayList完成一样。

我的主要代码:

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

适配器:
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();
    }
}

持有者:
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() + "");
    }


}

最佳答案

首先,Febi Mathew,感谢您为我提供的帮助。

我找到了解决方法。

我在Gradle中的原始行是

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

在我只为Recycler采样后,结果是一样的,我尝试了一个较旧的版本
compile 'com.android.support:recyclerview-v7:23.1.1'

而且效果很好。所以我想这不是我的错误=)。如果您知道如何报告此错误,欢迎您这样做。

更新:
出问题了,我没有将商品的layout-height设置为wrap内容。基于我对旧版本不了解的原因,这并不重要,但是在新版本上,它占用了我的所有屏幕,因此我不打算尝试将其向下滑动。

10-07 19:25