RecyclerView什么都没有显示

时间:2015-10-16 10:42:02

标签: android android-recyclerview recycler-adapter

我的适配器中有一些项目,但RecyclerView中没有显示任何内容。

适配器

public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {

    private List<WorkOrder> orders = new LinkedList<>();

    public void setData(List<WorkOrder> orders) {
        this.orders = orders;
    }

    @Override
    public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
        return new WorkOrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
        WorkOrder order = orders.get(position);
        holder.bind(order);
    }

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

}

ViewHolder

public class WorkOrderViewHolder extends RecyclerView.ViewHolder {

    private TextView title;
    private TextView description;
    private TextView date;

    public WorkOrderViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title_textview);
        description = (TextView) view.findViewById(R.id.description_textview);
        date = (TextView) view.findViewById(R.id.date_textview);
    }

    public void bind(WorkOrder order) {
        title.setText("Test");
        description.setText("Test");
        date.setText("Test");
    }
}

活动(使用AndroidAnnotations)

@EActivity(R.layout.activity_workorders)
@OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {

    @ViewById(R.id.orders_recyclerview)
    RecyclerView ordersList;

    List<WorkOrder> orders = new LinkedList<>();
    private WorkOrderAdapter adapter;

    {
        adapter = new WorkOrderAdapter();
        orders.add(new WorkOrder());
        orders.add(new WorkOrder());
        orders.add(new WorkOrder());
        adapter.setData(orders);
    }

    @AfterViews
    public void initViews() {
        ordersList.setAdapter(adapter);
    }

}

1 个答案:

答案 0 :(得分:15)

请将LayoutManager添加到RecyclerView,然后重试

ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);