Andrei Angurets Andrei Angurets - 5 months ago 28
Android Question

The last element in the row BrowseFragment

I have a Fragment:

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


In BrowseFragment I add several rows with items:

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);
}


I installed OnItemViewSelectedListener listener:

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

setOnItemViewSelectedListener(new ItemViewSelectedListener());


override method onItemSelected:

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


I need to detect when the last item in the row is selected. How do I determine it?

Answer

You can get all the information you need out of the Row row, Object item, and the ArrayObjectAdapter on the Row that are handed back in the onItemSelected(...) event.

Something like:

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
}