Keep focus on first item of the list and scroll the List

1.6k Views Asked by At

I am developing an Android TV application and showing the items on the list. When I scroll the list focus moving from the current item to the next, I want the keep focus constantly on the first item and scroll the whole list from the same selected item position. Like, this implementation of Hotstar. Currently, I am using Android ListRow with the following code.

AssetCardPresenter assetCardPresenter = new AssetCardPresenter(getActivity());
            MdsCompletedRecordingAdapter cardRowAdapter = new MdsCompletedRecordingAdapter(getActivity(), assetCardPresenter, 10);
            Map<String, MdsCompletedRecord> completedRecordingMap = getMdsCompletedRecordingProgramsMap(completedRecordedProgramList);
            HeaderItem headerItem = new HeaderItem(SCHEDULED_PROGRAM_HEADER_ID, COMPLETED_HEADER_NAME);
            ArrayList<MdsCompletedRecord> arrayList = new ArrayList<>(completedRecordingMap.values());
            Collections.reverse(arrayList);
            cardRowAdapter.addAllItems(arrayList);
            ListRow row = new ListRow(headerItem, cardRowAdapter);
            mRowAdapter.add(row);
2

There are 2 best solutions below

0
Sofiane Hassaini On

You can do this easily by using HorizontalGridView from Leanback and set these parameters to force the focus on the left of the screen

    windowAlignment = BaseGridView.WINDOW_ALIGN_BOTH_EDGE
    windowAlignmentOffset = 0f
    windowAlignmentOffsetPercent = 0f
    itemAlignmentOffsetPercent = 0f
0
Rados On

In your row's Presenter, you can get a reference to the row's HorizontalGridView and then use setWindowAlignmentOffsetPercent:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    
    horizontalGridView = (HorizontalGridView) parent.findViewById(R.id.row_content);
    int offset = 10;
    horizontalGridView.setWindowAlignmentOffsetPercent(offset);
    

    MyImageCardView cardView = new MyImageCardView(mContext);

    return new ViewHolder(cardView);
}