ViewPager喜欢AndroidTV中的BrowseFragment中的东西

时间:2016-09-22 08:13:45

标签: android android-fragments android-tv

我是Android TV的新手。请查看下面附图。

enter image description here

在这张照片的顶部有一个褶边,所有红色标记的区域都是图像。

我想在Android TV中制作此屏幕。我为此使用了BrowseFragment。我在First Row使用了两个Presenter,在另一行使用了另一个。

我使用过的代码:

HomeFragment.java

    private  CustomListRowPresenter mListRowPresenter;
    private void loadRows() {
        List<Movie> list = MovieList.setupMovies();
        mListRowPresenter = new CustomListRowPresenter(getActivity(),this);
        mRowsAdapter = new ArrayObjectAdapter(mListRowPresenter);
        CardPresenter cardPresenter = new CardPresenter(getActivity());

        mNumberOfRows = NUM_ROWS;
        HeaderItem gridHeader = new HeaderItem(0, "");
        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add(getResources().getString(R.string.grid_view));
        gridRowAdapter.add(getString(R.string.error_fragment));
        gridRowAdapter.add(getResources().getString(R.string.personal_settings));
        mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));

        int i;
        for (i = 1; i < NUM_ROWS; i++) {
            if (i != 0) {
                Collections.shuffle(list);
            }
            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
            for (int j = 1; j < NUM_COLS; j++) {
                listRowAdapter.add(list.get(j % 5));
            }
            HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
            mRowsAdapter.add(new ListRow(header, listRowAdapter));
        }



        setAdapter(mRowsAdapter);

    }

CardPresenter.java

public class CardPresenter extends Presenter {
    private static final String TAG = "CardPresenter";

    private static int CARD_WIDTH = 360;
    private static int CARD_HEIGHT = 160;
    private static int sSelectedBackgroundColor;
    private static int sDefaultBackgroundColor;
    private Drawable mDefaultCardImage;
    private Activity mActivity;

    public CardPresenter(Activity activity){
        mActivity = activity;
    }

    public CardPresenter(){
     }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        Log.d(TAG, "onCreateViewHolder");

        sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
        sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
        mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);
/*
        ImageCardView cardView = new ImageCardView(parent.getContext()) {
            @Override
            public void setSelected(boolean selected) {
                updateCardBackgroundColor(this, selected);
                super.setSelected(selected);
            }
        };

        cardView.setFocusable(true);
        cardView.setFocusableInTouchMode(true);
        cardView
                .setBackgroundResource(R.drawable.gridview_cell_border);
        updateCardBackgroundColor(cardView, false);*/

        View view = mActivity.getLayoutInflater().inflate(R.layout.gridview_cell_rows, parent,
                false);
        LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.container_grid_view_item);
        linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);

        view.setFocusable(true);

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

     }

    private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
        int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
        // Both background colors should be set because the view's background is temporarily visible
        // during animations.
        view.setBackgroundColor(color);
        view.findViewById(R.id.info_field).setBackgroundColor(color);
    }

    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
        Movie movie = (Movie) item;
        View cardView = (View) viewHolder.view;
        ImageView imageView = (ImageView)cardView.findViewById(R.id.imageView);
        LinearLayout linearLayout = (LinearLayout)cardView.findViewById(R.id.container_grid_view_item);
        linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);
        Log.d(TAG, "onBindViewHolder");
        if (movie.getCardImageUrl() != null) {
           /* cardView.setTitleText(movie.getTitle());
            cardView.setContentText(movie.getStudio());
            cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);*/
            Glide.with(viewHolder.view.getContext())
                    .load(movie.getCardImageUrl())
                    .centerCrop()
                    .error(mDefaultCardImage)
                    .into(imageView);
        }


    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
        Log.d(TAG, "onUnbindViewHolder");
        View cardView = (View) viewHolder.view;
        // Remove references to images so that the garbage collector can free up memory

    }

因此,对于第一行,我使用了CardPresenter,而对于其他行,我使用过GridPresenter。

我的要求是这个viewpager类型的东西应该每5秒自动旋转一次。

所以请帮助我如何设计这个片段。

1 个答案:

答案 0 :(得分:0)

Creating a Catalog Browser中所述,如果您希望自定义片段之间的标头视图,请使用Presenter并实现抽象方法来创建,绑定和取消绑定视图持有者。在显示目录浏览器的BrowseFragment实现中,使用setHeaderPresenterSelector()方法为行标题设置演示者,或使用setOnItemViewSelectedListener()方法设置项目选择侦听器,如以下示例所示

setHeaderPresenterSelector(new PresenterSelector() {
    @Override
    public Presenter getPresenter(Object o) {
        return new IconHeaderItemPresenter();
    }
});

然后,使用Timer设置每个页面展示者的持续时间,如GitHub post所示。

对于示例代码实现,此tutorial也可能会有所帮助。