androidandroid-fragmentsleanback

How to get selected/focused item or it's index in VerticalGridFragment class of Android?


I need to get the selected/focused item's index of the VerticalGridFragment. I found the method to set the selected item in the documentation https://developer.android.com/reference/android/support/v17/leanback/app/VerticalGridFragment.html, i.e. setSelectedPosition(int position) which Sets the selected item position.

But I couldn't find any method in documentation which provides the selected Item or it's index.

Can someone please help me to find a work around to get the selected item of a VerticalGridFragment?


Solution

  • You can't get the focusing position but you can get the focusing object. Below mothod setAdapter(mAdapter); you can write:

    setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
            @Override
            public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
                Object object = (Object) item;
                //can write somethings here
            }
        });