每四个项目点击回收器视图效果

时间:2016-09-12 10:28:21

标签: android android-recyclerview

当我点击缩略图时,我有一个recyclerviewonClicklistener我要移除ImageView并且它有效,但此效果也在列表中的每八个项目上,所以每8个缩略图都会消失。

我的适配器:

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);
            }
        }
    }
}

以下是活动中的用法:

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);

如果你有任何想法会很高兴听到他们:)

1 个答案:

答案 0 :(得分:0)

当您隐藏缩略图图像时,问题出在方法onBindViewHolder()中的代码

holder.thumbnailView.setVisibility(View.GONE);

Recyclerview每次都不使用新的布局实例,它会重复使用回收的布局。所以你应该玩show / hide。

在布局中设置缩略图图像,只显示/隐藏图像。

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

我希望,它会对你有帮助。

如果没有,请告诉我。