RecyclerView 项目专注于 Android TV

Lar*_*ara 5 android android-tv leanback

我正在开发Android TV一个应用程序,我有一个recycler view. 如何将焦点集中在想要的位置上?(我已经尝试过scrollToPositionsmoothScroller,但它不起作用)。提前致谢!

bur*_*akk 6

您必须在项目视图上调用setFocusable(true)和方法。setFocusableInTouchMode(true)

一个例子:

@NonNull
@Override
public ProgrammeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_programme, parent, false);
    view.setFocusable(true);
    view.setFocusableInTouchMode(true);

    sDefaultBackgroundColor = ContextCompat.getColor(parent.getContext(), android.R.color.transparent);
    sSelectedBackgroundColor = Color.parseColor("#66ffe680");

    updateEPGTitleRowBackgroundColor(view, false);

    view.setOnFocusChangeListener(mOnFocusChangeListener);
    view.setOnKeyListener(mOnKeyListener);

    return new ViewHolder(view);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

您可以尝试致电

mRecyclerView.getChildAt(0).requestFocus();
Run Code Online (Sandbox Code Playgroud)

聚焦特定项目的方法。只需将 0 替换为您想要的项目索引即可。