BrowseFragment行中的最后一个元素

时间:2016-06-13 08:38:18

标签: android android-fragments android-tv leanback

我有一个片段:

public class FilmFragment extends BrowseFragment implements LoaderManager.LoaderCallbacks<Object>

BrowseFragment 中,我添加了几行项目:

private void loadRubricsFilms(List<Rubric> mRubrics, List<Film> mFilms) {
    int j = 0;

    for (Rubric mRubric : mRubrics) {
        ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(mCardPresenter);

        for (Film mFilm : mFilms) {
            if (mFilm.getRubric() == mRubric.getId()) {
                listRowAdapter.add(mFilm);
            }
        }
        HeaderItem header = new HeaderItem(j++, mRubric.getTitle());
        mRowsAdapter.add(new ListRow(header, listRowAdapter));
    }
    setAdapter(mRowsAdapter);
}

我安装了 OnItemViewSelectedListener 监听器:

    private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
    @Override
    public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                               RowPresenter.ViewHolder rowViewHolder, Row row) {
    }
}

    setOnItemViewSelectedListener(new ItemViewSelectedListener());

覆盖方法 onItemSelected

    @Override
    public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                               RowPresenter.ViewHolder rowViewHolder, Row row) {
    }

我需要检测何时选择了行中的最后一项。我该如何确定?

1 个答案:

答案 0 :(得分:5)

您可以从Row rowObject item以及ArrayObjectAdapterRow获取所需的所有信息。onItemSelected(...)事件。

类似的东西:

final ListRow listRow = (ListRow) row;
final ArrayObjectAdapter currentRowAdapter = listRow.getAdapter();
int selectedIndex = currentRowAdapter.indexOf(item);
if (selectedIndex != -1 && (currentRowAdapter.size() - 1) == selectedIndex ) {
    // The last item was selected
}