适配器仅显示第一项

时间:2018-08-01 00:06:58

标签: android

我使用recyclerView来获取来自服务器的列表。此列表的大小为16,即16个项目。

但是在播放适配器时,仅显示第一项。我已经创建了一种方法来按位置填充它,但是它似乎不起作用。你能帮我吗?

片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_price_quote, container, false);
    mRecyclerView = view.findViewById(R.id.recycler_Quote);
    Bundle args = getArguments();
    if (args != null) {
        jsonQuotationResponse = (JsonQuotationResponse) args.getSerializable("cotacao");
        mAdapter = new CustomAdapterPriceQuote(getActivity(), jsonQuotationResponse.getData().getCotacoes());
        mRecyclerView.setAdapter(mAdapter);
        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
    }

    return view;
}

适配器:

public class CustomAdapterPriceQuote extends RecyclerView.Adapter<CustomAdapterPriceQuote.ViewHolder> {
    private Context mContext;
    private List<QuotationVehicleSellerModel> quotationVehicleSellerModelList;
    private View view;

    public CustomAdapterPriceQuote(Context context, List<QuotationVehicleSellerModel> quotationVehicleSellerModels) {
        mContext = context;
        quotationVehicleSellerModelList = quotationVehicleSellerModels;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(
            ViewGroup parent, int viewType) {
        // create a new view
        view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_price_quote, parent, false);
        // set the view's size, margins, paddings and layout parameters

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        QuotationVehicleSellerModel cotacoes = getCotacoesVo(position);
        if (cotacoes != null && cotacoes.getBuyer_name() != null) {
            holder.txtNameSeller.setText(cotacoes.getBuyer_name());
        }

    }

    @Override
    public int getItemCount() {
        if (quotationVehicleSellerModelList != null) {
            return quotationVehicleSellerModelList.size();
        }
        return 0;
    }

    private QuotationVehicleSellerModel getCotacoesVo(int position) {

        return quotationVehicleSellerModelList.get(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtNameSeller;

        public ViewHolder(View itemView) {
            super(itemView);

            txtNameSeller = itemView.findViewById(R.id.txtNameSeller_itemQuote);
        }
    }
}

仅该项目出现在位置0。所有不为null的项目都没有出现的原因是什么?谢谢

0 个答案:

没有答案