ViewHolder in RecyclerView.Adapter not defined for position

Below is my code for onBindViewHolder

(inside MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

)

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    StatusItem item = mDataset.get(position);
    //......
    //Add content and timing to the textview
    String content = item.getContent();

    holder.mTextViewTime.setText(timing);
    //Set the img
    holder.imgViewIcon.setImageDrawable(item.getProfileDrawable());
    //Set content image (for Instagram)
    holder.mImageViewContentPic.setImageDrawable(item.getContentDrawable());
    //HIDE THE VIEW Start
    if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }
    //HIDE THE VIEW End
}

      

Part HIDE THE VIEW

doesn't work as expected. When I scroll down the views work fine. However, when I start scrolling to the top, i.e. revisiting previous views, the ImageViews that should be VISIBLE

, become GONE

, although I checked my dataset and made sure it hasn't changed. Attempting to call other methods in the views also gives inconsistent results (positions and elements in the dataset do not match).

It seems that the view holders are not bound to specific positions inside the RecyclerView.

The code works as expected if I remove the part HIDE THE VIEW

. Is there a way to solve this and hide views dynamically in my case?

Note. I used some AsyncTasks to update the dataset and call notifyDataSetChanged()

if it matters.

+3


source to share


2 answers


###This is the solution to your problem:###

holder.mImageViewContentPic.setVisibility(View.VISIBLE);
if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }

      



+8


source


Since RecyclerView

uses the utility very well, ViewHolder

A could be ViewHolder

B, so you need to list each attribute ViewHolder

if some attributes are attached to the wrong object.



0


source







All Articles