Scroll to a given position in Android Leanback ListRow

I am using Google Leanback widgets in an Android TV app. It uses a RowsFragment with ListRows in it.

I am trying to determine if there is any way to programmatically scroll through a specific object within one of the lines. I delved into the docs for Leanback widgets, but can't find what I was looking for.

+6
source share
3 answers

I had a similar need: I needed to set the initial selected item to a ListRow. I ended up subclassing ListRowPresenter as follows:

import android.support.v17.leanback.widget.ListRowPresenter; import android.support.v17.leanback.widget.RowPresenter; public class CustomPresenter extends ListRowPresenter { private int mInitialSelectedPosition; public CustomPresenter(int position) { this.mInitialSelectedPosition = position; } @Override protected void onBindRowViewHolder(RowPresenter.ViewHolder holder, Object item) { super.onBindRowViewHolder(holder, item); ViewHolder vh = (ListRowPresenter.ViewHolder) holder; vh.getGridView().setSelectedPosition(mInitialSelectedPosition); } } 

Hope this helps you.

+5
source

In the latest version of Leanback (I think v23.3.0 +) you can now specify not only the position of the line, but also perform additional tasks in the line. In your case, the task will be a programmatic choice as follows:

BrowseFragment.setSelectedPosition(0, true, new ListRowPresenter.SelectItemViewHolderTask(2));

No need to implement a personalized list of strings or anything

+5
source

I did this when I needed to implement "Go back to the first item in the row by clicking Back."

I called this method from Activity onBackPressed () .

If this method returns false , we call Activity.super.onBackPressed (). If true , we do not.

  public boolean onBackPressed(){ boolean consumeBack; int selectedRowPosition = getRowsFragment().getSelectedPosition(); ListRowPresenter.ViewHolder selectedRow = (ListRowPresenter.ViewHolder) getRowsFragment().getRowViewHolder(selectedRowPosition); int selectedItemPosition = selectedRow.getSelectedPosition(); if(selectedItemPosition == 0){ consumeBack = false; } else { consumeBack = true; getRowsFragment().setSelectedPosition(selectedRowPosition, true, new ListRowPresenter.SelectItemViewHolderTask(0)); } return consumeBack; } 

Instead of "0" you can set any position you need.

+1
source

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


All Articles