How to use setRowViewSelected from ListRowPresenter

I am using the default project for Android TV. Below is the code for creating maps 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 do this because I do not want the first line map to be selected when the application starts. It should be selected only after pressing the user button on the Dpad. If I cannot do it this way, what should I do to get the behavior mentioned?

+5
source share
2 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 do not think that you can deselect all elements in BrowseFragment using this approach.

Try setting the ItemViewSelectedListener after loading the data instead of setting it to onActivityCreated so that all items are not selected on first run.

A possible reason why the top left line item will always be selected by default, and you cannot have all the unselected items on first run:

BrowseFragment onItemSelected method ( line 1372-1382 ) on initial mMainFragmentRowsAdapter.getSelectedPosition() start calls

  @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 set to your application's MainFragment .

Thus, the first time the 0th element in the 0th row is selected as the default selected element, but if you delay the mExternalOnItemViewSelectedListener setting, this call will not reach your selected listener for the first time.

+2
source

you can use this callback method.

  void onRowViewSelected (RowPresenter.ViewHolder vh, boolean selected) 

Called when this type of row changes the selection state. A subclass can override this to respond to selected row state changes. A subclass can create visual changes in the Row view, but should not create animations in the Row view.

 mListRowPresenter.setRowViewSelected(vh, false); 

why do you unselect first? I have not received your question. Clearly, can you explain what you want to do?

0
source

Source: https://habr.com/ru/post/1259501/


All Articles