Recyclerview Android

Asked

Viewed 71 times

0

I wanted to make imageButton appear if item.getImprimir() == true. It seems to work ok but if scrolling Recyclerview down starts to appear problems, it starts to appear right where item.getImprimir() == false. I know that recycleview updates the view as the user goes down or up, but I don’t know why it doesn’t work properly someone helps me 8).

public class AtletasAdapter extends RecyclerView.Adapter<AtletasAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClick(Atletas item);
}

private final List<Atletas> items;
private final OnItemClickListener listener;

public AtletasAdapter(List<Atletas> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}

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

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.bind(items.get(position), listener);
    holder.editar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), EditActvity.class);
            intent.putExtra("idInscricaoModalidade", items.get(position).getIdInscricaoModalidade());
            intent.putExtra("idModalidade", items.get(position).getIdModalidade());
            view.getContext().startActivities(new Intent[]{intent});
        }
    });
    holder.impressora.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AtletasClienteActivity.imprimir(view.getContext(), items.get(position).getIdInscricaoModalidade(), items.get(position).getNome(), AtletasActivity.spnPistas.getSelectedItem().toString(), items.get(position).getDq(), items.get(position).getNc());
        }
    });
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

    private TextView nome;
    private TextView divisao;
    private RelativeLayout relativeLayout;
    private ImageButton impressora;
    private ImageButton editar;

    public ViewHolder(View itemView) {
        super(itemView);
        nome = (TextView) itemView.findViewById(R.id.nome);
        divisao = (TextView) itemView.findViewById(R.id.divisao);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
        impressora = (ImageButton) itemView.findViewById(R.id.impressora);
        editar = (ImageButton) itemView.findViewById(R.id.editar);
    }

    public void bind(final Atletas item, final OnItemClickListener listener) {
        nome.setText(item.getNome());
        divisao.setText(item.getDivisao());
        relativeLayout.setBackgroundResource(item.getColor());

        if (item.getImprimir()) {
            impressora.setVisibility(View.VISIBLE);
        }
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(item);
            }
        });
    }

}

1 answer

3


You’re only treating when item.getImprimir() == true.
Recyclerview, when scrolling, reuses used views, they come with attributes with values previously "set".

So you have to treat when it’s false:

public void bind(final Atletas item, final OnItemClickListener listener) {
    nome.setText(item.getNome());
    divisao.setText(item.getDivisao());
    relativeLayout.setBackgroundResource(item.getColor());

    if (item.getImprimir()) {
        impressora.setVisibility(View.VISIBLE);
    }else{
        impressora.setVisibility(View.GONE);//ou View.INVISIBLE
    }
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(item);
        }
    });
}
  • That’s just now that I understand... The printer comes from the layout as android:visibility="Invisible" and I didn’t even notice what type the recyrcleview say so takes advantage of the layout that has already been rendered... Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.