Android TV 的 Leanback:取消设置视频标题会增加行间距
Leanback for Android TV : Unsetting Video Title Increases Spacing between Rows
sample app 的 PlaybackOverlayFragment 使用 PlaybackControlsGlue 来设置基于数据模型的播放控件。这是使用标准胶水时的样子:
我的问题是我不希望 title/subtitle 文本出现在主播放器控制栏上方 - 我们希望它们出现在播放器屏幕的左上角。因此,为了禁用 title/subtitle 的显示,我重写了胶水的 createControlsRowAndPresenter()
并改用 PlaybackControlsRowPresenter 的 empty-args 构造函数:
@Override
public PlaybackControlsRowPresenter createControlsRowAndPresenter() {
PlaybackControlsRow controlsRow = new PlaybackControlsRow(this);
setControlsRow(controlsRow);
final View.OnKeyListener onKeyListener = this;
PlaybackControlsRowPresenter presenter = new PlaybackControlsRowPresenter() { // no AbstractDetailsDescriptionPresenter argument
@Override
protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) {
super.onBindRowViewHolder(vh, item);
vh.setOnKeyListener(onKeyListener);
}
@Override
protected void onUnbindRowViewHolder(RowPresenter.ViewHolder vh) {
super.onUnbindRowViewHolder(vh);
vh.setOnKeyListener(null);
}
};
// secondaryActionsAdapter setup not shown
presenter.setOnActionClickedListener(new OnActionClickedListener() {
@Override
public void onActionClicked(Action action) {
dispatchAction(action);
}
});
return presenter;
}
结果呢? title/subtitle 未按预期显示,但现在主控件栏和其他行之间的间距更大:
我哪里做错了,还是 leanback 库的错误?
没有这两行文本,播放控件现在位于该视图的顶部。您可以对播放控件应用边距或填充以将其移动到预期位置。
事实证明,播放控件需要在其上方有一些视图,这样它们就不会占据其容器视图的顶部(@Nick 是对的)。但我想分享我的解决方案,以防有人有类似的需求。
PlaybackControlsRowPresenter 可以在其构造函数中接受任何演示者,而不仅仅是 AbstractDetailsDescriptionPresenters。所以 createControlsRowAndPresenter() 应该是这样的:
EmojiRowPresenter emojiRowPresenter = new EmojiRowPresenter() {
@Override
protected void onBindEmojiInfo(EmojiRowView rowView, EmojiInfo emojiInfo) {
rowView.setEmojiInfo(emojiInfo);
}
};
PlaybackControlsRowPresenter presenter = new PlaybackControlsRowPresenter(emojiRowPresenter) { // replace the default description presenter with custom presenter
...
}
// everything else stays as before
EmojiRowPresenter 是 Presenter 的子类,如下所示:
public abstract class EmojiRowPresenter extends Presenter {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
EmojiRowView emojiRowView = new EmojiRowView(parent.getContext());
emojiRowView.setFocusable(true);
emojiRowView.setFocusableInTouchMode(true);
return new ViewHolder(emojiRowView);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
EmojiRowView emojiRowView = (EmojiRowView) viewHolder.view;
PlaybackControlHelper glue = (PlaybackControlHelper) item;
EmojiInfo emojiInfo = glue.getEmojiInfo();
if (emojiInfo != null) {
onBindEmojiInfo(emojiRowView, emojiInfo);
}
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
// ...
}
protected abstract void onBindEmojiInfo(EmojiRowView rowView, EmojiInfo emojiInfo);
}
当然,EmojiRowView 从定义每个项目的布局创建视图。这是最终结果:
sample app 的 PlaybackOverlayFragment 使用 PlaybackControlsGlue 来设置基于数据模型的播放控件。这是使用标准胶水时的样子:
我的问题是我不希望 title/subtitle 文本出现在主播放器控制栏上方 - 我们希望它们出现在播放器屏幕的左上角。因此,为了禁用 title/subtitle 的显示,我重写了胶水的 createControlsRowAndPresenter()
并改用 PlaybackControlsRowPresenter 的 empty-args 构造函数:
@Override
public PlaybackControlsRowPresenter createControlsRowAndPresenter() {
PlaybackControlsRow controlsRow = new PlaybackControlsRow(this);
setControlsRow(controlsRow);
final View.OnKeyListener onKeyListener = this;
PlaybackControlsRowPresenter presenter = new PlaybackControlsRowPresenter() { // no AbstractDetailsDescriptionPresenter argument
@Override
protected void onBindRowViewHolder(RowPresenter.ViewHolder vh, Object item) {
super.onBindRowViewHolder(vh, item);
vh.setOnKeyListener(onKeyListener);
}
@Override
protected void onUnbindRowViewHolder(RowPresenter.ViewHolder vh) {
super.onUnbindRowViewHolder(vh);
vh.setOnKeyListener(null);
}
};
// secondaryActionsAdapter setup not shown
presenter.setOnActionClickedListener(new OnActionClickedListener() {
@Override
public void onActionClicked(Action action) {
dispatchAction(action);
}
});
return presenter;
}
结果呢? title/subtitle 未按预期显示,但现在主控件栏和其他行之间的间距更大:
我哪里做错了,还是 leanback 库的错误?
没有这两行文本,播放控件现在位于该视图的顶部。您可以对播放控件应用边距或填充以将其移动到预期位置。
事实证明,播放控件需要在其上方有一些视图,这样它们就不会占据其容器视图的顶部(@Nick 是对的)。但我想分享我的解决方案,以防有人有类似的需求。
PlaybackControlsRowPresenter 可以在其构造函数中接受任何演示者,而不仅仅是 AbstractDetailsDescriptionPresenters。所以 createControlsRowAndPresenter() 应该是这样的:
EmojiRowPresenter emojiRowPresenter = new EmojiRowPresenter() {
@Override
protected void onBindEmojiInfo(EmojiRowView rowView, EmojiInfo emojiInfo) {
rowView.setEmojiInfo(emojiInfo);
}
};
PlaybackControlsRowPresenter presenter = new PlaybackControlsRowPresenter(emojiRowPresenter) { // replace the default description presenter with custom presenter
...
}
// everything else stays as before
EmojiRowPresenter 是 Presenter 的子类,如下所示:
public abstract class EmojiRowPresenter extends Presenter {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
EmojiRowView emojiRowView = new EmojiRowView(parent.getContext());
emojiRowView.setFocusable(true);
emojiRowView.setFocusableInTouchMode(true);
return new ViewHolder(emojiRowView);
}
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
EmojiRowView emojiRowView = (EmojiRowView) viewHolder.view;
PlaybackControlHelper glue = (PlaybackControlHelper) item;
EmojiInfo emojiInfo = glue.getEmojiInfo();
if (emojiInfo != null) {
onBindEmojiInfo(emojiRowView, emojiInfo);
}
}
@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
// ...
}
protected abstract void onBindEmojiInfo(EmojiRowView rowView, EmojiInfo emojiInfo);
}
当然,EmojiRowView 从定义每个项目的布局创建视图。这是最终结果: