I'm trying to place a mark or place a badge in my cardviews placing it in the middle of my card. The problem is, if I place the image with setMainimage both images show at the same time, the way I thought it would've worked was that first one shows, then followed by the second (which is smaller than the first), but that was not the case. The second way I tried was placing it with the styles but I'm using the same style for all my cards so the image is showing in all my cards. I would like some advice or some other way to do it.
-
It's hard to understand the arrangement you want to achieve. Could you mock it up perhaps and show a sketch? Are you trying to show an **overlay** over the main image? – czak Jul 15 '16 at 15:21
-
indeed, i'm trying the LayoutInflater to achieve that ,but i'm not sure if it will work .... i'll just need to put an image infront of the main image of the cardview – Gil Larios Jul 15 '16 at 16:05
-
You can see a Badge Overlay implementation that works on API 18+ (JELLY_BEAN_MR2) here: https://stackoverflow.com/a/70624898/3079753 – Marcos Jan 07 '22 at 17:21
1 Answers
There are a couple different ways you can achieve this. A simple way would be to just supply your own XML in the Presenter. In your XML, you can overlay your elements on top of each other however you'd like. Check this SO post for how to achieve it.
@Override
final protected BaseCardView onCreateView(Context context) {
final BaseCardView cardView = new BaseCardView(context, null, R.style.YourCardStyle) {
@Override
public void setSelected(boolean selected) {
// TODO: Add your functionality you want here! Showing/hiding elements.
super.setSelected(selected);
}
};
cardView.addView(LayoutInflater.from(context).inflate(R.layout.card_your_custom_view, null));
// Just some init method to set up your views visibility.
initCardView(cardView);
return cardView;
}
Then as long as you name your views correctly in the XML, everything should work as you want. You'll bind the model to your views in onBindViewHolder
public void onBindViewHolder(CardModel cardModel, BaseCardView cardView) {
if (cardModel == null) {
return;
}
Video video = cardModel.clip;
Context context = cardView.getContext();
ImageView imageView = (ImageView) cardView.findViewById(R.id.main_image);
TextView primaryText = (TextView) cardView.findViewById(R.id.primary_text);
TextView secondaryText = (TextView) cardView.findViewById(R.id.secondary_text);
TextView extraText = (TextView) cardView.findViewById(R.id.extra_text);
}
With the above code blocks, you'll have to replace R.layout.card_your_custom_view
with your layout file which has View
s with IDs main_image
, primary_text
, secondary_text
, and 'extra_text. As well as replacing
R.style.YourCardStyle` with your own card style. Here are some examples of card styles you're allowed to use.
To get a better understanding of Presenters
, check out this folder in the Leanback Showcase app.
Please let me know if this works.
-
it look's like you are using the type of cards that exist ..(underinfo,extra,etc) making a new class with the basecard , not sure if is that or in my same card do what you just tell me ... – Gil Larios Jul 19 '16 at 17:21
-
This code makes use of the BaseCardView they provide. If you don't care about any of the transitions then you could entirely build your own (and it's rather simple). This approach lets you utilize some of their classes functionality while supplying your own layout which allows for overlapping views and custom onSelection transitions. – Kyle Venn Jul 19 '16 at 17:43
-
soooo it's making a whole new kind of card ? i get that , the problem is that i already seen that and for what i understand you need to make a whole new card extending the basecard that you are going to use.....maybe i'm wrong ,i'm not sure .... – Gil Larios Jul 19 '16 at 17:55
-
Correct, you'll be making a whole new kind of card which extends `BaseCardView` but you won't need a new class. You'll ONLY need a new layout (and optionally a style). You can re-use their card view and dynamically add your overlaying view, but that is subject to breaking if they change their CardView (so I don't suggest it). So I suggest making your own layout (card_your_custom_view). If you create a custom `Presenter` with the methods I specified above, everything should work. – Kyle Venn Jul 19 '16 at 18:11
-
ok ok ok i'll try it , i'm half way there ,already have my new layout .... – Gil Larios Jul 19 '16 at 18:16
-
That's great! Try out my suggestion and let me know if you hit any issues. – Kyle Venn Jul 19 '16 at 18:27
-
ok i'm stuck hahahaha i have my style,my xml but now how do i show it ? – Gil Larios Jul 19 '16 at 23:04
-
sorry i have never been so deep in the design sooo ,it's kind of hard ,i'm not sure how to show the variables that i just make ... like i just said ,i got the style an the xml .... i just need to show it ,maybe it's a silly question but i'm having troubles with it ... ill post what i've got in a net line cause this thing has a limit – Gil Larios Jul 19 '16 at 23:11
-
@Override public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { MovieRent movieRent = (MovieRent) item; BaseCardView cardView = (BaseCardView) viewHolder.view; if (viewHolder == null) { return; } Context context = cardView.getContext(); ImageView mainImage = (ImageView) cardView.findViewById(R.id.main_image); ImageView icono = (ImageView)cardView.findViewById(R.id.icono); TextView nameMovie(TextView)cardView.findViewById(R.id.name_movie); } – Gil Larios Jul 19 '16 at 23:19
-
Looks good, it feels like it's getting close! Okay so is this `onBind` method inside your custom presenter? If so, then all you need to do is pass your presenter to the `ArrayObjectAdapter` like [here](https://github.com/googlesamples/leanback-showcase/blob/master/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/page/PageAndListRowFragment.java#L184). Let me know if that works. – Kyle Venn Jul 20 '16 at 14:20
-
i found this widget in the view of the cards ... "android.support.v17.leanback.widget.NonOverlappingRelativeLayout" i'm not sure if would be a problem ..... – Gil Larios Jul 20 '16 at 16:15
-
-
Yes, but the old code is very similar (as it relates to showing cards). The old sample app has a more simple implementation and the new showcase has a more complicated example. But once you understand how the Cards are being manipulated (through XML as well as presenters), then it makes it very easy to customize. You might even want to just open up their sample project but drop in your custom presenter (since it should work with a little tweaking). – Kyle Venn Jul 20 '16 at 19:56
-
@GilLarios Awesome! Since it sounds like this answer solved your problem, can you please click the check mark to accept my answer as the correct one? Thanks! – Kyle Venn Jul 21 '16 at 14:18
-
-
No problem! People on StackOverflow (SO) like getting the imaginary points which you get by upvoting people's posts (the up arrow) and by having your answers "accepted" (checkmark). You usually upvote questions/answers you think are pretty good. And clicking the checkmark marks this question as "resolved" so other people know a solution exists for the question you asked. – Kyle Venn Jul 21 '16 at 18:15