在上一课中,您创建了一个在浏览片段中实现的目录浏览器,它显示媒体项目的列表。在本课中,您将为媒体项目创建卡片视图,并在浏览片段中呈现它们。
类及其子类显示与媒体项目关联的元数据。本课中使用的 BaseCardView
类显示内容的图像以及媒体项目的标题。ImageCardView
另请参阅 Leanback 示例应用 中的示例实现。
创建卡片呈现器
会根据需要生成视图并将对象绑定到视图。在您的应用向用户呈现其内容的浏览片段中,您将为内容卡片创建 Presenter
,并将其传递给将内容添加到屏幕的适配器。在以下代码中,Presenter
是在 CardPresenter
回调中创建的,该回调来自 onLoadFinished()
LoaderManager
Kotlin
override fun onLoadFinished(loader: Loader<HashMap<String, List<Movie>>>, data: HashMap<String, List<Movie>>) { rowsAdapter = ArrayObjectAdapter(ListRowPresenter()) val cardPresenter = CardPresenter() var i = 0L data.entries.forEach { entry -> val listRowAdapter = ArrayObjectAdapter(cardPresenter).apply { entry.value.forEach { movie -> add(movie) } } val header = HeaderItem(i, entry.key) i++ rowsAdapter.add(ListRow(header, listRowAdapter)) } val gridHeader = HeaderItem(i, getString(R.string.more_samples)) val gridRowAdapter = ArrayObjectAdapter(GridItemPresenter()).apply { add(getString(R.string.grid_view)) add(getString(R.string.error_fragment)) add(getString(R.string.personal_settings)) } rowsAdapter.add(ListRow(gridHeader, gridRowAdapter)) adapter = rowsAdapter updateRecommendations() }
Java
@Override public void onLoadFinished(Loader<HashMap<String, List<Movie>>> arg0, HashMap<String, List<Movie>> data) { rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); CardPresenter cardPresenter = new CardPresenter(); int i = 0; for (Map.Entry<String, List<Movie>> entry : data.entrySet()) { ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter); List<Movie> list = entry.getValue(); for (int j = 0; j < list.size(); j++) { listRowAdapter.add(list.get(j)); } HeaderItem header = new HeaderItem(i, entry.getKey()); i++; rowsAdapter.add(new ListRow(header, listRowAdapter)); } HeaderItem gridHeader = new HeaderItem(i, getString(R.string.more_samples)); GridItemPresenter gridPresenter = new GridItemPresenter(); ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(gridPresenter); gridRowAdapter.add(getString(R.string.grid_view)); gridRowAdapter.add(getString(R.string.error_fragment)); gridRowAdapter.add(getString(R.string.personal_settings)); rowsAdapter.add(new ListRow(gridHeader, gridRowAdapter)); setAdapter(rowsAdapter); updateRecommendations(); }
创建卡片视图
在此步骤中,您将使用卡片视图的视图持有者构建卡片呈现器,该视图持有者描述您的媒体内容项目。请注意,每个呈现器只能创建一种视图类型。如果您有两种卡片视图类型,那么您需要两个卡片呈现器。
在
中,实现一个 Presenter
回调,它会创建一个可用于显示内容项目的视图持有者onCreateViewHolder()
Kotlin
private const val CARD_WIDTH = 313 private const val CARD_HEIGHT = 176 class CardPresenter : Presenter() { private lateinit var mContext: Context private lateinit var defaultCardImage: Drawable override fun onCreateViewHolder(parent: ViewGroup): Presenter.ViewHolder { mContext = parent.context defaultCardImage = mContext.resources.getDrawable(R.drawable.movie) ...
Java
@Override public class CardPresenter extends Presenter { private Context context; private static int CARD_WIDTH = 313; private static int CARD_HEIGHT = 176; private Drawable defaultCardImage; @Override public ViewHolder onCreateViewHolder(ViewGroup parent) { context = parent.getContext(); defaultCardImage = context.getResources().getDrawable(R.drawable.movie); ...
在
方法中,为内容项目创建卡片视图。以下示例使用 onCreateViewHolder()
。ImageCardView
当选择一张卡片时,默认行为是将其扩展到更大的尺寸。如果您想为选定的卡片指定不同的颜色,请按如下所示调用 setSelected()
。
Kotlin
... val cardView = object : ImageCardView(context) { override fun setSelected(selected: Boolean) { val selected_background = context.resources.getColor(R.color.detail_background) val default_background = context.resources.getColor(R.color.default_background) val color = if (selected) selected_background else default_background findViewById<View>(R.id.info_field).setBackgroundColor(color) super.setSelected(selected) } } ...
Java
... ImageCardView cardView = new ImageCardView(context) { @Override public void setSelected(boolean selected) { int selected_background = context.getResources().getColor(R.color.detail_background); int default_background = context.getResources().getColor(R.color.default_background); int color = selected ? selected_background : default_background; findViewById(R.id.info_field).setBackgroundColor(color); super.setSelected(selected); } }; ...
当用户打开您的应用时,Presenter.ViewHolder
将显示您内容项的 CardView
对象。您需要通过调用 setFocusable(true)
和 setFocusableInTouchMode(true)
来设置这些对象接收来自 D-pad 控制器的焦点,如以下代码所示。
Kotlin
... cardView.isFocusable = true cardView.isFocusableInTouchMode = true return ViewHolder(cardView) }
Java
... cardView.setFocusable(true); cardView.setFocusableInTouchMode(true); return new ViewHolder(cardView); }
当用户选择 ImageCardView
时,它会展开以显示其文本区域,背景颜色是您指定的,如图 1 所示。