Skip to content

Show images in ListViews using CardView and RecyclerView

rutura edited this page Apr 16, 2017 · 1 revision
  • Quick Code:
@Override
    public void onBindViewHolder(final CardViewHolder holder, int position) {
        String title = context.getString(R.string.title_text);
        holder.titleView.setText(title);
        holder.descriptionView.setText(R.string.ipsum_lorem);
        if (selectedPosition == position) {
            holder.descriptionView.setVisibility(View.VISIBLE);
        } else {
            holder.descriptionView.setVisibility(View.GONE);
        }
        holder.imageView.setImageResource(R.drawable.photo);
        Palette.from(bitmap)
                .generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        Palette.Swatch swatch = palette.getDarkVibrantSwatch();
                        if (swatch == null) {
                            swatch = palette.getSwatches().get(0);
                        }
                        int titleTextColor = Color.WHITE;
                        if (swatch != null) {
                            titleTextColor = swatch.getTitleTextColor();
                            titleTextColor = ColorUtils.setAlphaComponent(titleTextColor, 255);
                        }
                        holder.titleView.setTextColor(titleTextColor);
                    }
                });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (selectedPosition == position) {
                    selectedPosition = -1;
                    notifyItemChanged(position);
                } else {
                    int oldSelectedPosition = selectedPosition;
                    selectedPosition = position;
                    notifyItemChanged(oldSelectedPosition);
                    notifyItemChanged(selectedPosition);
                }
            }
        });
    }
Clone this wiki locally