Tito
Tito

Reputation: 478

OnClick in recycler view effects every eight item

I have a recyclerview and onClicklistener when I click on the thumbnail I want to remove the ImageView and it works but also this effect is on every eight item in the list, so every 8th thumbnail disappear.

My adapter:

public class BarListAdapter extends RecyclerView.Adapter<BarListAdapter.BarListViewHolder> {

    private List<Bar> bars;
    private int rowLayout;
    private Context context;
    private BarViewHolderClicks clickListener;

    public BarListAdapter(List<Bar> bars, int rowLayout, Context context, BarViewHolderClicks clickListener) {
        this.bars = bars;
        this.rowLayout = rowLayout;
        this.context = context;
        this.clickListener = clickListener;
    }

    @Override
    public BarListAdapter.BarListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        return new BarListViewHolder(view, clickListener);
    }

    @Override
    public void onBindViewHolder(final BarListViewHolder holder, final int position) {
        holder.barNameView.setText(bars.get(position).getName());
        holder.position = position;
        holder.lastVideoTimeView.setText(bars.get(position).getCaptureTime());
        if (bars.get(position).isWatched()) {
           holder.thumbnailView.setVisibility(View.GONE);
        } else {
            setThumbnail(holder, position);
        }
    }

    public void changeStatusToWatched(int position){
        bars.get(position).setWatched(true);
        notifyDataSetChanged();
    }

    private void setThumbnail(BarListViewHolder holder, int position) {
        Picasso.with(context).load(bars.get(position).getThumbnailUrl())
                .placeholder(R.drawable.webcam).error(R.drawable.webcam)
                .resize(50, 50).centerCrop().into(holder.thumbnailView);
    }

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

    public static class BarListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public BarViewHolderClicks clickListener;
        View itemLayout;
        TextView barNameView;
        TextView lastVideoTimeView;
        RoundedImageView thumbnailView;
        CheckBox barCheckbox;
        int position;

        public BarListViewHolder(View v, BarViewHolderClicks listener) {
            super(v);
            this.clickListener = listener;
            barNameView = (TextView) v.findViewById(R.id.bar_name);
            thumbnailView = (RoundedImageView) v.findViewById(R.id.video_thumbnail);
            itemLayout = v.findViewById(R.id.item_lahyout);
            lastVideoTimeView = (TextView) v.findViewById(R.id.last_video_time);
            itemLayout.setOnClickListener(this);
            thumbnailView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v instanceof ImageView) {
                clickListener.onThumbnailClick(position);
            } else {
                clickListener.onLayoutClick(position);
            }
        }
    }
}

And here is the usage in the activity:

final List<Bar> bars = application.getAllBars();
        int topBarsSize = application.getTopBars().size();

        BarViewHolderClicks clickListener = new BarViewHolderClicks() {
            @Override
            public void onLayoutClick(int position) {
                SMSLogger.i("on layout click for position " + position);
                Intent intent = new Intent(MainActivity.this, BarDetailsActivity.class);

                intent.putExtra(IntentConstants.SELECTED_BAR_POSITION, position);
                startActivity(intent);
            }

            @Override
            public void onThumbnailClick(int position) {
                SMSLogger.i("on thumbnail click for position " + position);
                barListAdapter.changeStatusToWatched(position);
                Intent intent = new Intent(MainActivity.this, PlayVideoActivity.class);
                intent.putExtra(IntentConstants.SELECTED_BAR_POSITION, position);
                startActivity(intent);
            }
        };

        barListAdapter =
                new BarListAdapter(bars, R.layout.bar_item_layout, this, clickListener);

        recViewBars.setAdapter(barListAdapter);

If you have any ideas will be nice to hear them :)

Upvotes: 1

Views: 282

Answers (1)

Sachin Saxena
Sachin Saxena

Reputation: 604

The problem is with your code inside the method onBindViewHolder(), when you hide the thumbnail image

holder.thumbnailView.setVisibility(View.GONE);

Recyclerview does not use a new layout instance every time, it reuses the recycled layout. So you should play with show/hide.

Set the thumbnail image in layout and simply show/hide image.

if (bars.get(position).isWatched()) {
   holder.thumbnailView.setVisibility(View.GONE);
} else {
    holder.thumbnailView.setVisibility(View.VISIBLE);
}

I hope, it will help you.

If not, please let me know.

Upvotes: 1

Related Questions