Android: RecyclerView content messed up after scrolling [closed] Android: RecyclerView content messed up after scrolling [closed] android android

Android: RecyclerView content messed up after scrolling [closed]


After battling with this same issue for about 24 hours, I found a solution that worked for me. The key was using the setIsRecyclable() method of RecyclerView.ViewHolder class.

Here is a section of my onBindViewHolder() code.

@Overridepublic void onBindViewHolder(final MyViewHolder holder, int position) {    final DataSource dataSource = dataSourceList.get(position);    holder.setIsRecyclable(false);    holder.name.setText(dataSource.getName());    holder.active.setChecked(dataSource.getActive());    String logoStr = dataSource.getLogo();    //Logo    /**     * Do all the logo insertion stunts here     */    /**     * Register the changes to the Switch     */    holder.active.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){            dataSource.setActive(isChecked);        }    });}


onBindHolder() is called several times as recycler needs a view, unless there's a new one when view type is changed.So each time you set visibility in child views, other views states are also changing.

Whenever you scroll up and down, these views are getting re-drawn with wrong visibility options.

Solution :

You have a setValue method check values and set to view. If neccessary it calls another method "showView". You need to implement else statement (which is value is 0 or null) and hideView there...

void setValue(Object value, TextView textView, TableRow row, View seperator) {    if (value != null) {        if (!isEmpty(value.toString())) {            textView.setText(String.valueOf(value));            showViews(row, seperator);        }    } else        hideViews(row, seperator);}private void showViews(TableRow row, View seperator) {    row.setVisibility(View.VISIBLE);    seperator.setVisibility(View.VISIBLE);}private void hideViews(TableRow row, View seperator) {    row.setVisibility(View.INVISIBLE); // if there is a empty space change it with View.GONE    seperator.setVisibility(View.INVISIBLE);}


onBindHolder called several times as Recycler View needs a view unless new one. So each time you set visilibity in child views, other views states are also changes.

Whenever you scroll up and down, these views are getting re-drawed with wrong visibility options so always specify both the conditions cause recycler view doesn't know the previous state/conditions/values of our widgets.

Solution :

If in If block you set visibility of any android widget.setVisibility(View.Gone) then in else block you have to set it's visibility opposite vwith widget.setVisibility(View.Visible) to overcome the above problem.

 @Overridepublic void onBindViewHolder(ViewHolder viewHolder, int i) {    viewHolder.tvName.setText(ModelCategoryProducts.name.get(i));    viewHolder.tvPrice.setText("Rs."+String.format("%.2f", Float.parseFloat(ModelCategoryProducts.price.get(i))));    if(ModelCategoryProducts.special_price.get(i).equals("null")) {        viewHolder.tvSpecialPrice.setVisibility(View.GONE); // here visibility is gone and in else it's opposite visibility i set.        viewHolder.tvPrice.setTextColor(Color.parseColor("#ff0000"));        viewHolder.tvPrice.setPaintFlags(0);// here paint flag is 0 and in else it's opposite flag that i want is set.    }else if(!ModelCategoryProducts.special_price.get(i).equals("null")){        viewHolder.tvPrice.setTextColor(Color.parseColor("#E0E0E0"));        viewHolder.tvSpecialPrice.setVisibility(View.VISIBLE);        viewHolder.tvSpecialPrice.setText("Rs." + String.format("%.2f", Float.parseFloat(ModelCategoryProducts.special_price.get(i))));        viewHolder.tvPrice.setPaintFlags(viewHolder.tvPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);    }    if (!ModelCategoryProducts.image_url.get(i).isEmpty()) {        Picasso.with(context)                .load(ModelCategoryProducts.image_url.get(i))                .into(viewHolder.ivProduct);    }    viewHolder.setClickListener(new ItemClickListener() {        @Override        public void onClick(View view, int position, boolean isLongClick) {            if (isLongClick) {//                    Toast.makeText(context, "#" + position + " - " + ModelCategoryProducts.name.get(position) + " (Long click)", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(context, "#" + position + " - " + ModelCategoryProducts.name.get(position), Toast.LENGTH_SHORT).show();                Intent i = new Intent(context, ProductDetail.class);                i.putExtra("position",position);                i.putExtra("flagHlvCheck", 5);                context.startActivity(i);            }        }    });}