在上一课中,您创建了一个在浏览 fragment 中实现的目录浏览器, ,显示媒体项列表。在本课中,您将为媒体项创建卡片视图 呈现它们。
BaseCardView
类及其子类显示与媒体项关联的元数据。通过
ImageCardView
类会显示内容的图片以及媒体项的标题。
另请参阅 <ph type="x-smartling-placeholder"></ph> Leanback 示例应用 ,了解所有最新动态。
创建卡片 Presenter
Presenter
生成视图并向视图绑定对象
。在应用向用户呈现内容的浏览 fragment 中,您可以创建一个
为内容卡片使用 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 和视图持有者, 媒体内容。请注意,每个 Presenter 只能创建一个视图类型。如果您有两个 卡片视图类型,则需要两个卡片 Presenter。
在 Presenter
中,实现一个
onCreateViewHolder()
创建可用于显示内容项的 ViewHolder 的回调:
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)
,
如以下代码所示:
Kotlin
... cardView.isFocusable = true cardView.isFocusableInTouchMode = true return ViewHolder(cardView) }
Java
... cardView.setFocusable(true); cardView.setFocusableInTouchMode(true); return new ViewHolder(cardView); }
当用户选择 ImageCardView
时,它会展开
以采用您指定的背景颜色显示其文本区域,如图 1 所示。