Android Recyclerview cache

Asked

Viewed 79 times

0

I have a recyrcleview on most devices worked well, but on some devices version 7, it’s a bit buggy, it’s kind of cached with the first elements. Does anyone have any idea? example of normal working photo more case is rolled it starts bugging. I thank you for your attention

inserir a descrição da imagem aqui

public class AlvosAdapter extends RecyclerView.Adapter<AlvosAdapter.ViewHolder> {
int TEMPO_CLICK_BUTTON = 400;

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


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


public AlvosAdapter(List<AlvosResultado> 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_alvos, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.bind(items.get(position), listener);
    holder.alfa.setTag(position);
    holder.delta.setTag(position);
    holder.charlie.setTag(position);
    holder.procedure.setTag(position);
    holder.nooshoot.setTag(position);
    holder.miss.setTag(position);

    holder.alfa.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    if (holder.alvo.getText().subSequence(0, 1).equals("T") || holder.alvo.getText().subSequence(0, 2).equals("DT")) {
                        String a = String.valueOf(Integer.parseInt(holder.alfa.getText().toString()) + 1);
                        items.get(position).setAlfa(String.valueOf(Integer.parseInt(items.get(position).getAlfa()) + 1));
                        holder.alfa.setText(a);
                        AlvosActivity.alvos[0] += 1;
                    } else {
                        String a = String.valueOf(Integer.parseInt(holder.alfa.getText().toString()) + 1);
                        if (holder.miss.getText().equals("1")) {
                            holder.miss.setText("0");
                            holder.alfa.setText("1");
                            addAcertoAlvo(0);
                            tirarAcertoAlvo(3);
                            items.get(position).setMiss("0");
                            items.get(position).setAlfa("1");
                            AlvosActivity.preencherTotalPontos(AlvosActivity.btMiss, AlvosActivity.alvos[3].toString());
                        } else if (holder.alfa.getText().equals("1")) {
                            mensagemAlvos(v.getContext());
                        } else {
                            items.get(position).setAlfa(String.valueOf(Integer.parseInt(items.get(position).getAlfa()) + 1));
                            holder.alfa.setText(a);
                            addAcertoAlvo(0);
                        }
                    }
                } else {
                    if (Integer.parseInt(holder.alfa.getText().toString()) >= 0) {
                        AlvosActivity.alvos[0] -= Integer.parseInt(holder.alfa.getText().toString());
                        items.get(position).setAlfa("0");
                        holder.alfa.setText("0");
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                AlvosActivity.preencherTotalPontos(AlvosActivity.btAlfa, AlvosActivity.alvos[0].toString());
                color(holder);
            }
            return true;
        }
    });
    holder.charlie.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    if (holder.alvo.getText().subSequence(0, 1).equals("T") || holder.alvo.getText().subSequence(0, 2).equals("DT")) {
                        String a = String.valueOf(Integer.parseInt(holder.charlie.getText().toString()) + 1);
                        holder.charlie.setText(a);
                        AlvosActivity.alvos[1] += 1;
                        items.get(position).setCharlie(String.valueOf(Integer.parseInt(items.get(position).getCharlie()) + 1));
                    }
                } else {
                    if (Integer.parseInt(holder.charlie.getText().toString()) >= 0) {
                        AlvosActivity.alvos[1] -= Integer.parseInt(holder.charlie.getText().toString());
                        items.get(position).setCharlie("0");
                        holder.charlie.setText("0");
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                color(holder);
                AlvosActivity.preencherTotalPontos(AlvosActivity.btCharlie, AlvosActivity.alvos[1].toString());
            }
            return true;
        }
    });
    holder.delta.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    if (holder.alvo.getText().subSequence(0, 1).equals("T") || holder.alvo.getText().subSequence(0, 2).equals("DT")) {
                        String a = String.valueOf(Integer.parseInt(holder.delta.getText().toString()) + 1);
                        holder.delta.setText(a);
                        AlvosActivity.alvos[2] += 1;
                        items.get(position).setDelta(String.valueOf(Integer.parseInt(items.get(position).getDelta()) + 1));
                    }
                } else {
                    if (Integer.parseInt(holder.delta.getText().toString()) >= 0) {
                        AlvosActivity.alvos[2] -= Integer.parseInt(holder.delta.getText().toString());
                        items.get(position).setDelta("0");
                        holder.delta.setText("0");
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                AlvosActivity.preencherTotalPontos(AlvosActivity.btDelta, AlvosActivity.alvos[2].toString());
                color(holder);
            }
            return true;
        }
    });

    holder.miss.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    if (holder.alvo.getText().subSequence(0, 1).equals("T") || holder.alvo.getText().subSequence(0, 2).equals("DT")) {
                        String a = String.valueOf(Integer.parseInt(holder.miss.getText().toString()) + 1);
                        holder.miss.setText(a);
                        AlvosActivity.alvos[3] += 1;
                        items.get(position).setMiss(String.valueOf(Integer.parseInt(items.get(position).getMiss()) + 1));
                    } else {
                        String a = String.valueOf(Integer.parseInt(holder.miss.getText().toString()) + 1);
                        if (holder.alfa.getText().equals("1")) {
                            holder.alfa.setText("0");
                            holder.miss.setText("1");
                            addAcertoAlvo(3);
                            tirarAcertoAlvo(0);
                            items.get(position).setMiss("1");
                            items.get(position).setAlfa("0");
                            AlvosActivity.preencherTotalPontos(AlvosActivity.btAlfa, AlvosActivity.alvos[0].toString());
                        } else if (holder.miss.getText().equals("1")) {
                            mensagemAlvos(v.getContext());
                        } else {
                            AlvosActivity.alvos[3] += 1;
                            holder.miss.setText(a);
                            items.get(position).setMiss(String.valueOf(Integer.parseInt(items.get(position).getMiss()) + 1));
                        }
                    }
                } else {
                    if (Integer.parseInt(holder.miss.getText().toString()) >= 0) {
                        AlvosActivity.alvos[3] -= Integer.parseInt(holder.miss.getText().toString());
                        items.get(position).setMiss("0");
                        holder.miss.setText("0");
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                AlvosActivity.preencherTotalPontos(AlvosActivity.btMiss, AlvosActivity.alvos[3].toString());
                color(holder);
            }
            return true;
        }
    });
    holder.nooshoot.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    String a = String.valueOf(Integer.parseInt(holder.nooshoot.getText().toString()) + 1);
                    holder.nooshoot.setText(a);
                    AlvosActivity.alvos[4] += 1;
                    items.get(position).setNooShoot(String.valueOf(Integer.parseInt(items.get(position).getNooShoot()) + 1));
                } else {
                    if (Integer.parseInt(holder.nooshoot.getText().toString()) >= 0) {
                        AlvosActivity.alvos[4] -= Integer.parseInt(holder.nooshoot.getText().toString());
                        items.get(position).setNooShoot("0");
                        holder.nooshoot.setText("0");
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                AlvosActivity.preencherTotalPontos(AlvosActivity.btNoshoot, AlvosActivity.alvos[4].toString());
            }
            return true;
        }
    });
    holder.procedure.setOnTouchListener(new View.OnTouchListener() {
        long start = 0, finish = 0, total = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                start = System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                finish = System.currentTimeMillis();
                total = finish - start;
                if (total <= TEMPO_CLICK_BUTTON) {
                    String a = String.valueOf(Integer.parseInt(holder.procedure.getText().toString()) + 1);
                    holder.procedure.setText(a);
                    AlvosActivity.alvos[5] += 1;
                    items.get(position).setProcedure(String.valueOf(Integer.parseInt(items.get(position).getProcedure()) + 1));
                } else {
                    if (Integer.parseInt(holder.procedure.getText().toString()) >= 0) {
                        AlvosActivity.alvos[5] -= Integer.parseInt(holder.procedure.getText().toString());
                        items.get(position).setProcedure("0");
                        holder.procedure.setText("0");
                        AlvosActivity.checkSelecionadoZerar();
                    }
                }
                AlvosActivity.checkSelecionadoZerar();
                AlvosActivity.calcularHitFactor();
                AlvosActivity.preencherTotalPontos(AlvosActivity.btProcedure, AlvosActivity.alvos[5].toString());
            }
            return true;
        }
    });
}

public static void mensagemAlvos(Context context) {
    Toast toast = Toast.makeText(context,
            "Alvo não pode ter mais de 1 hits", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.show();
}

public static void addAcertoAlvo(int pos) {
    try {
        AlvosActivity.alvos[pos] += 1;
    } catch (Exception e) {
        AlvosActivity.alvos[pos] = 1;
    }
}

public void tirarAcertoAlvo(int pos) {
    try {
        AlvosActivity.alvos[pos] -= 1;
    } catch (Exception e) {
        AlvosActivity.alvos[pos] = 0;
    }
}

public void color(ViewHolder holder) {
    if (holder.alvo.getText().subSequence(0, 1).equals("T") || holder.alvo.getText().subSequence(0, 2).equals("DT")) {
        int soma = Integer.parseInt(holder.alfa.getText().toString()) + Integer.parseInt(holder.delta.getText().toString()) + Integer.parseInt(holder.charlie.getText().toString()) + Integer.parseInt(holder.miss.getText().toString());
        if (soma >= 3) {
            mudarCorFundoPapeis(R.drawable.cell_red, holder.alfa, holder.delta, holder.charlie, holder.miss);
        } else if (soma == 1) {
            mudarCorFundoPapeis(R.drawable.cell_green, holder.alfa, holder.delta, holder.charlie, holder.miss);
        } else if (soma == 2) {
            mudarCorFundoPapeis(R.drawable.cell_green2, holder.alfa, holder.delta, holder.charlie, holder.miss);
        } else {
            mudarCorFundoPapeis(R.drawable.cell_shape, holder.alfa, holder.delta, holder.charlie, holder.miss);
        }
    } else {
        int soma = Integer.parseInt(holder.alfa.getText().toString()) + Integer.parseInt(holder.miss.getText().toString());
        if (soma >= 1) {
            mudarCorFundoMetais(R.drawable.cell_green2, holder.alfa, holder.miss);
        } else {
            mudarCorFundoMetais(R.drawable.cell_shape, holder.alfa, holder.miss);
        }
    }
}

public static void mudarCorFundoPapeis(int drawable, Button alfa, Button delta, Button charlie, Button miss) {
    alfa.setBackgroundResource(drawable);
    delta.setBackgroundResource(drawable);
    charlie.setBackgroundResource(drawable);
    miss.setBackgroundResource(drawable);
}

public static void mudarCorFundoMetais(int drawable, Button alfa, Button miss) {
    alfa.setBackgroundResource(drawable);
    miss.setBackgroundResource(drawable);
}

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

static class ViewHolder extends RecyclerView.ViewHolder {

    private Button alvo;
    private Button alfa;
    private Button delta;
    private Button charlie;
    private Button miss;
    private Button nooshoot;
    private Button procedure;

    public ViewHolder(View itemView) {
        super(itemView);
        alvo = itemView.findViewById(R.id.btAlvo);
        alfa = itemView.findViewById(R.id.btAlfa);
        delta = itemView.findViewById(R.id.btDelta);
        charlie = itemView.findViewById(R.id.btCharlie);
        miss = itemView.findViewById(R.id.btMiss);
        nooshoot = itemView.findViewById(R.id.btNoShoot);
        procedure = itemView.findViewById(R.id.btProcedure);
    }

    public void bind(final AlvosResultado item, final OnItemClickListener listener) {
        AlvosActivity.recycler.setRecycledViewPool(new RecyclerView.RecycledViewPool());
        AlvosActivity.recycler.getRecycledViewPool().clear();
        alvo.setText(item.getAlvo());
        alfa.setText(item.getAlfa());
        charlie.setText(item.getCharlie());
        delta.setText(item.getDelta());
        miss.setText(item.getMiss());
        nooshoot.setText(item.getNooShoot());
        procedure.setText(item.getProcedure());
        if (!alvo.getText().subSequence(0, 1).equals("T") && !alvo.getText().subSequence(0, 2).equals("DT")) {
            int soma = Integer.parseInt(item.getAlfa()) + Integer.parseInt(item.getMiss());
            delta.setEnabled(false);
            PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
            delta.getBackground().setColorFilter(colorFilter);
            delta.setText("");
            charlie.setEnabled(false);
            charlie.getBackground().setColorFilter(colorFilter);
            charlie.setText("");
            if (soma >= 1) {
                mudarCorFundoMetais(R.drawable.cell_green2, alfa, miss);
            }
        } else {
            int soma = Integer.parseInt(item.getAlfa()) + Integer.parseInt(item.getMiss()) + Integer.parseInt(item.getDelta()) + Integer.parseInt(item.getCharlie());
            if (soma >= 3) {
                mudarCorFundoPapeis(R.drawable.cell_red, alfa, delta, charlie, miss);
            } else if (soma == 1) {
                mudarCorFundoPapeis(R.drawable.cell_green, alfa, delta, charlie, miss);
            } else if (soma == 2) {
                mudarCorFundoPapeis(R.drawable.cell_green2, alfa, delta, charlie, miss);
            } else {
                mudarCorFundoPapeis(R.drawable.cell_shape, alfa, delta, charlie, miss);
            }
        }
    }
}

the items

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:shrinkColumns="*"
android:stretchColumns="*">

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/btAlvo"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="P1"
        android:layout_marginRight="3px"/>

    <Button
        android:id="@+id/btAlfa"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:text="A1"
        android:layout_marginLeft="3px"/>

    <Button
        android:id="@+id/btCharlie"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:text="A1" />

    <Button
        android:layout_marginRight="3px"
        android:id="@+id/btDelta"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="C1" />

    <Button
        android:layout_marginLeft="3px"
        android:id="@+id/btMiss"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="D1" />

    <Button
        android:id="@+id/btNoShoot"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="M1" />

    <Button
        android:id="@+id/btProcedure"
        android:layout_weight="1"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="10dip"
        android:text="N1"
        android:layout_marginRight="6px"/>
</TableRow>

  • I got in the final Else I put this and it worked

  • delta.setEnabled(true);&#xA; charlie.setEnabled(true);&#xA; delta.setBackgroundResource(R.color.colorBackground);&#xA; charlie.setBackgroundResource(R.color.colorBackground);

No answers

Browser other questions tagged

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