0

I use recyclerView to get a list coming from the server. This list has the size 16, that is, 16 items.

But when playing to the adapter, only shows the first item. I've created a method for it to fill by position but it does not seem to work. can you help me?

Fragment:

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

Adapter:

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

Only the item appears in position 0. What is the reason for all items that are not null, do not appear? Thank you

  • 1
    Can you scroll it? That is, are you sure that your items just aren't the same height as the `RecyclerView`, so the rest are off-screen? – Mike M. Aug 01 '18 at 00:12
  • Yeah, it happens. :-) Just change your item layout's root `View`'s `layout_height` from `match_parent` to `wrap_content`. Cheers! – Mike M. Aug 01 '18 at 00:19

0 Answers0