Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setRowViewSelected of ListRowPresenter

I am using default project for Android TV. Following is the code for creating cards in my BrowseFragment:

private void loadRows() {
    List<Movie> list = MovieList.setupMovies();
    ListRowPresenter mListRowPresenter = new ListRowPresenter();
    mRowsAdapter = new ArrayObjectAdapter(mListRowPresenter);

    mListRowPresenter.setRowViewSelected(/*HOW TO GET VIEWHOLDER HERE?*/, false);

    CardPresenter cardPresenter = new CardPresenter();

    int i;
    for (i = 0; i < NUM_ROWS; i++) {
        if (i != 0) {
            Collections.shuffle(list);
        }
        ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
        for (int j = 0; 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);
}

I am doing this as I don't want to make first card of row get selected when I launch app. It should only get selected after user press down button on Dpad. If I can't do it this way, what should I do to get mentioned behavior?

like image 951
uniruddh Avatar asked Oct 29 '22 17:10

uniruddh


1 Answers

You can setRowViewSelected by subclassing ListRowPresenter and overriding initializeRowViewHolder(RowPresenter.ViewHolder holder)

@Override
protected void initializeRowViewHolder(RowPresenter.ViewHolder holder) {
    super.initializeRowViewHolder(holder);

    setRowViewSelected(holder, false);
}

But I don't think you can unselect all items in BrowseFragment using this approach.

Try setting your ItemViewSelectedListener after your data is loaded instead of setting in onActivityCreated to have all items unselected on initial launch.

Possible reason why top left item of row will always get selected by default and you cannot have all unselected items on initial launch:

BrowseFragment's onItemSelected method (line 1372-1382) on initial launch calls mMainFragmentRowsAdapter.getSelectedPosition()

    @Override
    public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
            RowPresenter.ViewHolder rowViewHolder, Row row) {
        int position = mMainFragmentRowsAdapter.getSelectedPosition(); //<--
        if (DEBUG) Log.v(TAG, "row selected position " + position);
        onRowSelected(position);
        if (mExternalOnItemViewSelectedListener != null) {  //<--
            mExternalOnItemViewSelectedListener.onItemSelected(itemViewHolder, item,
                    rowViewHolder, row);
        }
    }

where getSelectedPosition() always returns 0 (line 483-485)

    public int getSelectedPosition() {
        return 0;
    }

It also calls mExternalOnItemViewSelectedListener.onItemSelected where mExternalOnItemViewSelectedListener is the ItemViewSelectedListener that you set in MainFragment of your app.

So on initial launch, 0th item in 0th row gets selected as a default selected item but if you delay setting mExternalOnItemViewSelectedListener this call will not reach your item selected listener the first time.

like image 135
random Avatar answered Nov 14 '22 19:11

random