in car_app_library/showcase/common/src/main/java/androidx/car/app/sample/showcase/common/templates/GridTemplateDemoScreen.java [88:195]
private GridItem createGridItem(int index) {
switch (index) {
case 0:
// Grid item with an icon and a title.
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mIcon).build(), GridItem.IMAGE_TYPE_ICON)
.setTitle("Non-actionable")
.build();
case 1:
// Grid item with an icon, a title, onClickListener and no text.
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mIcon).build(), GridItem.IMAGE_TYPE_ICON)
.setTitle("Second Item")
.setOnClickListener(
() -> CarToast.makeText(
getCarContext(),
"Clicked second item",
LENGTH_SHORT)
.show())
.build();
case 2:
// Grid item with an icon marked as icon, a title, a text and a toggle in
// unchecked state.
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mIcon).build(), GridItem.IMAGE_TYPE_ICON)
.setTitle("Third Item")
.setText(mThirdItemToggleState ? "Checked" : "Unchecked")
.setOnClickListener(
() -> {
mThirdItemToggleState = !mThirdItemToggleState;
CarToast.makeText(
getCarContext(),
"Third item checked: " + mThirdItemToggleState,
LENGTH_SHORT)
.show();
invalidate();
})
.build();
case 3:
// Grid item with an image, a title, a long text and a toggle that takes some
// time to
// update.
if (mIsFourthItemLoading) {
return new GridItem.Builder()
.setTitle("Fourth")
.setText(mFourthItemToggleState ? "On" : "Off")
.setLoading(true)
.build();
} else {
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mImage).build())
.setTitle("Fourth")
.setText(mFourthItemToggleState ? "On" : "Off")
.setOnClickListener(this::triggerFourthItemLoading)
.build();
}
case 4:
// Grid item with a large image, a long title, no text and a toggle in unchecked
// state.
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mImage).build(), GridItem.IMAGE_TYPE_LARGE)
.setTitle("Fifth Item has a long title set")
.setOnClickListener(
() -> {
mFifthItemToggleState = !mFifthItemToggleState;
CarToast.makeText(
getCarContext(),
"Fifth item checked: " + mFifthItemToggleState,
LENGTH_SHORT)
.show();
invalidate();
})
.build();
case 5:
// Grid item with an image marked as an icon, a long title, a long text and
// onClickListener.
return
new GridItem.Builder()
.setImage(new CarIcon.Builder(mIcon).build(),
GridItem.IMAGE_TYPE_ICON)
.setTitle("Sixth Item has a long title set")
.setText("Sixth Item has a long text set")
.setOnClickListener(
() ->
CarToast.makeText(
getCarContext(),
"Clicked sixth item",
LENGTH_SHORT)
.show())
.build();
default:
String titleText = (index + 1) + "th item";
String toastText = "Clicked " + (index + 1) + "th item";
return new GridItem.Builder()
.setImage(new CarIcon.Builder(mIcon).build(),
GridItem.IMAGE_TYPE_ICON)
.setTitle(titleText)
.setOnClickListener(
() ->
CarToast.makeText(
getCarContext(),
toastText,
LENGTH_SHORT)
.show())
.build();
}
}