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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With