如何选择,如主页的推荐,如何使电视项目可扩展?

时间:2015-07-07 08:24:13

标签: android android-tv

在AndroidTV的主页中,当用户选择推荐视频时,该项目将自动展开并显示比其他未选择项目更多的信息。

屏幕截图在这里:

enter image description here

在我的电视应用程序中,我将VerticalGridFragment扩展为GridFragment以显示电视节目。并且还扩展了Presenter以设置项目布局。但是,当选择项目时,项目只会扩大,但不会像主页推荐那样显示更多信息。

如何在我的电视应用程序的演示者中获得此效果?

2 个答案:

答案 0 :(得分:4)

也许我找到了解决方案:在Presenter中,我们会创建一个ViewHolder来展示,也许是这样:

public class GridItemPresenter extends Presenter {
......

public GridItemPresenter(Fragment fragment) {
    this.mainFragment = fragment;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    // Here is the view we want to show.
    final RichCardView cardView = new RichCardView(parent.getContext());

    // We should change something when the view is focused: show more information.
    cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, final boolean isFocused) {
            updateCardBackgroundColor(cardView, isFocused);
            cardView.setSelected(isFocused);
        }
    });
    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    ......
    return new ViewHolder(cardView);
}

private static void updateCardBackgroundColor(RichCardView view, boolean selected) {
    ......
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
    RichCardView cardView = (RichCardView) viewHolder.view;
    ......
}

@Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
    RichCardView cardView = (RichCardView) viewHolder.view;
    ......
}
}

ViewHolder是一个容纳展示视图的容器,在代码中,视图为RichCardView。以下是一些RichCardView代码:

public class RichCardView extends RelativeLayout {

public RichCardView(Context context) {
    this(context, null);
}

public RichCardView(Context context, AttributeSet attrs) {
    this(context, attrs, android.support.v17.leanback.R.attr.imageCardViewStyle);
}

public RichCardView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.lb_rich_card_view, this);

   ......
}

@Override
public void setSelected(boolean selected) {
    super.setSelected(selected);
    if (selected) {
        // when be selected, set some views Visible to show more information.
        mContentView.setVisibility(View.VISIBLE);
        mEpisodeView.setVisibility(View.VISIBLE);
    } else {
        // when not be selected, hide those views.
        mContentView.setVisibility(View.GONE);
        mEpisodeView.setVisibility(View.GONE);
    }
}
}

选择视图后,我们会显示更多视图以显示更多信息。否则,我们隐藏一些视图来隐藏一些信息。

是的,RichCardView只是扩展RelativeLayout。您还可以扩展您想要的任何ViewGroup。如果您想要改变生动,只需在更改视图的可见性时添加一些动画。

答案 1 :(得分:3)

您可以参考ImageCardView的实现。它有一个方法 setInfoVisibility setExtraVisibility ,它控制可扩展的CardView的行为。当CARD_REGION_VISIBLE_ACTIVATED设置为参数时,它将表示当用户选择标题时该区域不会出现,并且当用户移动到RowsFragment时将显示该区域。

更新(2015.10.26)

只有在选择CARD_REGION_VISIBLE_SELECTED时才能展开卡片。

下面是示例代码(已更新),应在Presenter类中实现。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    Log.d(TAG, "onCreateViewHolder");
    mContext = parent.getContext();

    ImageCardView cardView = new ImageCardView(mContext);
    cardView.setCardType(BaseCardView.CARD_TYPE_INFO_UNDER);
    cardView.setInfoVisibility(BaseCardView.CARD_REGION_VISIBLE_SELECTED);
    cardView.setFocusable(true);
    cardView.setFocusableInTouchMode(true);
    cardView.setBackgroundColor(mContext.getResources().getColor(R.color.fastlane_background));
    return new ViewHolder(cardView);
}

详情在this tutorial的底部解释。