2

I am working on the TV app and trying to use leanback support library as much as possible. The issue I ran into is that I am trying to add a View on top of the ImageView in ImageCardView. I would like to have my custom View centered on top of that image.

I've tried simply adding the View on 0 index but it just pushes the image down and sits on top of it. I see the ImageCardView is FrameLayout but it seems that FrameLayout is changed so all the children are vertically aligned.

I've also tried to fetch a parent of ImageCardView, which is ShadowOverlayContainer, and add the View there but it still doesn't show.

Example for regular card:

enter image description here

What I am trying to accomplish: enter image description here

Any suggestions how to add that View? Thank you.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Astagron
  • 277
  • 4
  • 13
  • Have you tried to modify the `Layout` it from `Leanback Library` itself ? – Jay Rathod May 24 '16 at 13:34
  • @jaydroider, you mean to create my own and replace `leanback` one? – Astagron May 24 '16 at 15:23
  • 1
    Modify the `Image Card View` `Layout` from `Lean Back Library` which is exist in `SDK` it self.It will do the trick for you. – Jay Rathod May 24 '16 at 16:24
  • Are you getting my point or not ? if not then i will suggest you how to do that. I have modified that `Layout` as per requirement. So let me know. – Jay Rathod May 25 '16 at 05:18
  • @jaydroider, didn't really go in that direction at the end. I removed that functionality from my card. If you have a working solution, you can post it as an answer and I can mark it as accepted :) There will probably be other people who would want to implement it this way. – Astagron Jun 08 '16 at 21:26
  • Currently I am running into this same problem. If you found the solution, could you please post it here. Thanks. – Vpd Dec 17 '16 at 01:07

2 Answers2

0

I think this works.

onBindViewHolder(){

.

..

...

int count = ((ViewGroup)((ImageCardView) viewHolder.view).getMainImageView().getParent().getParent()).getChildCount();

((ViewGroup)((ImageCardView) 
viewHolder.view).getMainImageView().getParent().getParent()).addView(xxx, count);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
  • Please provide an explanation with your code as to why you think this works. This will help the OP and future visitors to understand. – Bugs May 26 '17 at 08:57
0

Since API Level 18 (JELLY_BEAN_MR2) you can use a ViewOverlay

https://developer.android.com/reference/android/view/ViewOverlay

https://developer.android.com/reference/android/view/View#getOverlay()

  • get a Drawable drawable = context.getDrawable(R.drawable.sample)
  • set its Bounds drawable.setBounds(0, 0, 50, 50)
  • add an Overlay view.getOverlay().add(drawable)
public class ImageCardViewPresenter extends Presenter {
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        ImageCardView imageCardView = new ImageCardView(parent.getContext());
        imageCardView.setMainImageDimensions(100, 100);
        return new ImageCardViewHolder(imageCardView);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        Model model = (Model) item;
        ImageCardView imageCardView = (ImageCardView) viewHolder.view;
        Context context = imageCardView.getContext();

        imageCardView.setTitleText(model.getTitle());
        imageCardView.setMainImage(context.getDrawable(model.getImageId());

        Drawable badge = context.getDrawable(model.getBadgeId());
        badge.setBounds(0, 0, 20, 20);
        imageCardView.getOverlay().clear();
        imageCardView.getOverlay().add(badge);
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {
        ImageCardView imageCardView = (ImageCardView) viewHolder.view;
        imageCardView.setMainImage(null);
        imageCardView.getOverlay().clear();
    }

    public static class ImageCardViewHolder extends ViewHolder {
        public ImageCardViewHolder(View view) {
            super(view);
        }
    }
}
public class Model {
    private String title;

    @DrawableRes
    private int imageId;

    @DrawableRes
    private int badgeId;

    // getters and setters
}
Marcos
  • 3,263
  • 1
  • 23
  • 22