Miguel Barra
Miguel Barra

Reputation: 43

Visibility of items not working in recyclerView

I have around 12 CardViews. Each one set the visibility of ImageView at GONE depending about if the "high" variable is true or false but when I scroll down and then scroll up some ImageView widgets disappear and I have to mention that the last one ImageView isn't shown either.

@Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Currency currency = currencies.get(i);

        Double valor_actual = currencies.get(i).getValores().get(0);
        Double valor_anterior = currencies.get(i).getValores().get(1);

        viewHolder.textValue.setText(valor_actual.toString());

        BigDecimal bigDecimal = null;
        if (currency.getHigh()) {
            bigDecimal = new BigDecimal(valor_actual).subtract(new BigDecimal(valor_anterior));
            viewHolder.textInfo.setTextColor(Color.parseColor("#ff669900"));
            // doesn't work properly
            viewHolder.imageDown.setVisibility(View.GONE);
        }
        if (!currency.getHigh()){
            bigDecimal = new BigDecimal(valor_anterior).subtract(new BigDecimal(valor_actual));
            viewHolder.textInfo.setTextColor(Color.parseColor("#ffcc0000"));
            // doesn't work properly
            viewHolder.imageUp.setVisibility(View.GONE);
        }

        String unidad_medida = currency.getUnidad_medida();
        if (bigDecimal != null) {
            try {
                StringBuilder sb = new StringBuilder(currencyFormat(bigDecimal));
                if (unidad_medida.equalsIgnoreCase("porcentaje")) {
                    sb.deleteCharAt(0);
                    sb.insert(0, "%");
                }
                viewHolder.textInfo.setText(sb.toString());
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
        viewHolder.textName.setText(currency.getNombre());
    }

here works great

enter image description here

but when I scroll down and back to scroll up the third CardView doesn't show the ImageView :

enter image description here

Upvotes: 3

Views: 5909

Answers (4)

S. Sargsyan
S. Sargsyan

Reputation: 53

When you scroll, the same view which has been used for displaying item at position 0 may be reused to display item on position e.g 5 if item at position 0 is not visible anymore (this way recycle view avoids new view creation by using already created but not visible views).

So let's consider a scenario when we end up with having no image on view.

Item at position 0 should show up icon and item at position 5 should show down icon. And when you scroll recyclerview reuses view created for pos 0 to display view at pos 5. So basically you end up calling viewHolder.imageUp.setVisibility(View.GONE) and viewHolder.imageDown.setVisibility(View.GONE); on the same View.

Solutions is to manipulate visibility of the other view as well like:

viewHolder.imageUp.setVisibility(View.GONE);

viewHolder.imageDown.setVisibility(View.VISIBLE);

Upvotes: 1

Mallikarjuna
Mallikarjuna

Reputation: 884

the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.

RecyclerView Uses view Reusable that may be imageview or textview

So Better to Use BaseAdapter for Small set of Data

Refer Documentation

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Upvotes: 0

Gaurav Singh
Gaurav Singh

Reputation: 2320

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Currency currency = currencies.get(i);

    Double valor_actual = currencies.get(i).getValores().get(0);
    Double valor_anterior = currencies.get(i).getValores().get(1);

    viewHolder.textValue.setText(valor_actual.toString());

    BigDecimal bigDecimal = null;
    if (currency.getHigh()) {
        bigDecimal = new BigDecimal(valor_actual).subtract(new BigDecimal(valor_anterior));
        viewHolder.textInfo.setTextColor(Color.parseColor("#ff669900"));

        viewHolder.imageDown.setVisibility(View.GONE);
        viewHolder.imageUp.setVisibility(View.VISIBLE);
    } else {
        bigDecimal = new BigDecimal(valor_anterior).subtract(new BigDecimal(valor_actual));
        viewHolder.textInfo.setTextColor(Color.parseColor("#ffcc0000"));

        viewHolder.imageUp.setVisibility(View.GONE);
        viewHolder.imageDown.setVisibility(View.VISIBLE);
    }

    String unidad_medida = currency.getUnidad_medida();
    if (bigDecimal != null) {
        try {
            StringBuilder sb = new StringBuilder(currencyFormat(bigDecimal));
            if (unidad_medida.equalsIgnoreCase("porcentaje")) {
                sb.deleteCharAt(0);
                sb.insert(0, "%");
            }
            viewHolder.textInfo.setText(sb.toString());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
    viewHolder.textName.setText(currency.getNombre());
}

Upvotes: 0

Anjal Saneen
Anjal Saneen

Reputation: 3219

you need to put Visible in else condition also. Do this for each of the items you've got there as the list item in case of you're setting their visibilities at runtime.

if (currency.getHigh()) {
     bigDecimal = new BigDecimal(valor_actual).subtract(new BigDecimal(valor_anterior));
     viewHolder.textInfo.setTextColor(Color.parseColor("#ff669900"));
     // doesn't work properly
     viewHolder.imageDown.setVisibility(View.GONE);
     viewHolder.imageUp.setVisibility(View.VISIBLE);  
}
else {
     bigDecimal = new BigDecimal(valor_anterior).subtract(new BigDecimal(valor_actual));
     viewHolder.textInfo.setTextColor(Color.parseColor("#ffcc0000"));
     // doesn't work properly
     viewHolder.imageDown.setVisibility(View.VISIBLE);
     viewHolder.imageUp.setVisibility(View.GONE); 
 }

Upvotes: 4

Related Questions