dimanche 3 mai 2015

CardView containing image with round corners using RoundCornersDrawable class

I created a list of Cards with the new CardView and RecyclerView that look this way:

My card

I've tried to modify the card with round corners using this example, but as you can see from the screenshot I am only able to set the card's corner while the image remains with squared ones.

In my Adapter I have:

@Override
public ContactViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    final View itemView = LayoutInflater
                    .from(viewGroup.getContext())
                    .inflate(R.layout.item_card_view, viewGroup, false);
    CardView cardView = (CardView) itemView.findViewById(R.id.card_view);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.hImage);

    Bitmap mBitmap = BitmapFactory.decodeResource(itemView.getResources(), R.drawable.background);
    //Default
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        //Default
        imageView.setBackgroundResource(R.drawable.background);
    } else {
        //RoundCorners
        madapps.hellogridview.RoundCornersDrawable round = new madapps.hellogridview.RoundCornersDrawable(
                mBitmap,            
             itemView.getResources().getDimension(R.dimen.cardview_default_radius)
                , 5); //or your custom radius

        cardView.setPreventCornerOverlap(false); //it is very important

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            imageView.setBackground(round);
        else
            imageView.setBackgroundDrawable(round);
    }

    //Set onClick listener
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int tag = (Integer) v.findViewById(R.id.hTitle).getTag();
            Toast.makeText(viewGroup.getContext(), "Clickable card no: "+tag, Toast.LENGTH_LONG).show();
        }
    });
    return new ContactViewHolder(itemView);
}

and I've overridden the value of R.dimen.cardview_default_radiusto 8dp.

Aucun commentaire:

Enregistrer un commentaire