本文介绍了选择首页后,如何使电视项目可扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In AndroidTV's homepage, when user selects a recommendation video, the item will expand itself and shows more information than the other unselect items.

屏幕截图在这里:

在电视应用程序中,我将 VerticalGridFragment 扩展为 GridFragment 以显示电视节目.并且还扩展了 Presenter 以设置项目布局.但是,选择项目后,该项目只会放大,而不会像首页的建议那样显示更多信息.

In my TV application, I extends the VerticalGridFragment as the GridFragment to show TV shows. And also extends the Presenter to set the item layout. However, when item is selected, the item only scale bigger, but not show more information just like homepage's recommendation.

如何在电视应用程序的Presenter中获得这种效果?

How can I get this effect in my TV application's Presenter?

推荐答案

也许我找到了解决方案:在 Presenter 中,我们将创建一个 ViewHolder 进行显示,也许像这个:

Maybe I found the solution: In Presenter , we will create a ViewHolder to show, maybe like this:

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 代码:

ViewHolder is a container which holds the showing view, in upon code , view is RichCardView. Here are some of the RichCardView codes:

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);
    }
}
}

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

When the view is selected , we show more views to show more information. Otherwise, we hide some views to hide some information.

是的, RichCardView 只是对 RelativeLayout 的扩展.您还可以扩展所需的任何 ViewGroup .如果要进行生动的更改,只需在更改视图的可见性时添加一些动画即可.

And yes, RichCardView is simply extends RelativeLayout. You can also extend any ViewGroup you want. If you want to change lively, just add some animations when change view's visibility.

这篇关于选择首页后,如何使电视项目可扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:50
查看更多