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

4

6

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?

Underling answered 23/6, 2017 at 8:9 Comment(8)
why cant you use clickListener for that particular itemRakia
Thanks Rahul. I can't use that because I need to perform some operation if first row of the verticalGridFragment is focused and user presses some key. I don't need to perform the same action for all items.Underling
if you use some flag in list<model>, will this help ?Rakia
I don't think so.Underling
Seem you need a global variable to store selected position, and this setOnItemViewSelectedListener is fired when an item is selected. See docs hereLaidlaw
@Laidlaw I want the index or item. Please have a look at the question again.Underling
I think the row or item is enough, you can get position of them from your adapter or your list objects ? I just give the idea, because I did not working with this class before. You can look in the params of onItemSelected method from the interfaceLaidlaw
use arrayObjectAdapter.getIndex(object) will give you index of selected item.Tamarisk
B
0

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
        }
    });
Biles answered 22/9, 2017 at 3:1 Comment(1)
We can get the position. Do not provide misleading fact please!Fourfold
T
6

You get selected item position .

private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
            int position=  mAdapter.indexOf(item);

        }
}
Tamarisk answered 24/2, 2018 at 10:34 Comment(0)
B
4

You can use OnItemViewSelectedListener interface in Leanback for receiving notifications when a row or item becomes selected.

See the concept of the current selection is different than focus. A row or item can be selected without having focus. For example, when a row header view gains focus then the corresponding row view becomes selected.

mRowsAdapter is your ArrayObjectAdapter, few castings are required to get the current row index and current item index.

So first you get the rows adapter and by casting your rows adapter to ArrayObjectAdapter you get the adapter(which is nothing but rows adapter only) that is holding items in these rows.

       OnItemViewSelectedListener { itemViewHolder, item, rowViewHolder, row ->

            val indexOfRow = mRowsAdapter.indexOf(row)
            val indexOfItem = ((row as ListRow).adapter as ArrayObjectAdapter).indexOf(item)
            Toast.makeText(context, "$indexOfItem-$indexOfRow", Toast.LENGTH_SHORT).show()

            }
        }
Barramunda answered 7/3, 2020 at 10:1 Comment(0)
B
0

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
        }
    });
Biles answered 22/9, 2017 at 3:1 Comment(1)
We can get the position. Do not provide misleading fact please!Fourfold
C
0

Get selected/focused item or it's index in leanback fragment,

setOnItemViewSelectedListener { itemViewHolder, item, rowViewHolder, row ->
            if (item is AssetData) {
                val indexOfItem = ((row as ListRow).adapter as ArrayObjectAdapter).indexOf(item)
                selectedPosition!!.getItemPosition(indexOfItem)
            }
        }
Calcification answered 31/8, 2022 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.