我正在开发一个Android电视应用程序,并显示列表中的项目。当我将列表焦点从当前项目滚动到下一个项目时,我希望始终将焦点保持在第一个项目上,并从相同的选定项目位置滚动整个列表。比如Hotstar的这个实现。目前,我正在使用以下代码的Android ListRow。
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);
您可以通过使用Leanback的HorizontalGridView轻松完成此操作,并设置这些参数以强制将焦点放在屏幕左侧
windowAlignment = BaseGridView.WINDOW_ALIGN_BOTH_EDGE
windowAlignmentOffset = 0f
windowAlignmentOffsetPercent = 0f
itemAlignmentOffsetPercent = 0f
在行的Presenter中,您可以获得对行的HorizontalGridView的引用,然后使用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);
}