RecyclerView Android

0

I wanted to make the imageButton appear in case item.getPrint () == true. It seems to work okay but if scrolling the RecyclerView down starts appearing problems, it starts appearing even where item.getPrint () == false. I know that recycleview will update the view as the user goes down or up, but I do not know why it does not work correctly. 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);
            }
        });
    }

}
    
asked by anonymous 07.02.2018 / 18:53

1 answer

3

You're only dealing when item.getImprimir() == true . RecyclerView, when scrolling, reuses previously used views, they come with attributes with previously set values.

So, you must also treat when it is 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);
        }
    });
}
    
07.02.2018 / 19:00