Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TV: RowsFragment item click not working in few cases

I'm working on an Android TV application and I'm facing a weird issue related to the item click listeners: everything works fine but sometimes the click is not propagated, very likely the listener is not set.

I have subclassed RowsFragment and I'm displaying my images with rows of 6 elements: it means each row of the grid is actually a row in the adapter, even if visually the photos have the same month. Please have a look at the image to have a better understanding.

enter image description here

Now the issue: when I start the application and I scroll down, the first row which is not visible in the image above (so the fourth row, since here 3 rows are rendered), doesn't get my "clicks", nothing happens. This might be true also for the 5th, 6th, ... rows but then (I didn't recognize a pattern) rows start to be clickable again (i.e. 8th, 9th, ..) The first 3 rows are always clickable as well.

If I scroll up again and those rows which were not clickable appear on the screen again, now they are clickable.

In the onCreate of the fragment, I call:
setOnItemViewClickedListener(new ItemViewClickedListener());
and according to the documentation this should override the listener set by the single item view. It's also suggested to set one listener, not both: in fact I just set this one for the fragment.

In the Presenter class of the single row item, I have tried to add a click listener in the onCreateViewHolder for the view and I have found out when the issue occurs, this listener is called.

So it seems under some circumstances, the overall fragment listener does not override the view listener or it's not set at all.

What also worries me about this issue, is that I'm not able to reproduce it in debug mode when I set breakpoints and I stop the scrolling animation.

I'm using the latest Leanback version:

compile 'com.android.support:leanback-v17:25.3.1'

The issue is reproducible on the Nvidia Shield TV (Android 7.0), not sure on other devices.

Update: what's also interesting is the fact that when an item is not clickable in one row, I would expect all the items in the same row to be not clickable. Instead it's not the case, some are clickable, some are not.

Update 2: it seems ListRowPresenterItemBridgeAdapter method onBind, in some occasions, find getOnItemViewClickedListener to return null, so the item click listener is not set. That's weird because the listener is set for the main RowsFragment class and it works fine for most of the items, also items in the same row of those ones not working.

As a workaround I have found this solution: subclass the ListRowPresenter and set a dummy click listener to it.

private class GroupedListRowPresenter extends ListRowPresenter {
    GroupedListRowPresenter(int focusZoomFactor) {
        super(focusZoomFactor);
    }

    @Override
    protected void onBindRowViewHolder(RowPresenter.ViewHolder holder, Object item) {
        super.onBindRowViewHolder(holder, item);

        holder.setOnItemViewClickedListener(new BaseOnItemViewClickedListener() {
            @Override
            public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder
                    rowViewHolder, Object row) {
                Timber.d("item click from dummy listener: this should never happen!");
            }
        });
    }
}

I'm not comfortable with it, because I really don't like to set an empty click listener, even if I know it will get overridden.

Update 3: the issue is also reproducible with the sample application created by Android Studio, so it seems not related to something wrong in my code. For this reason I have filed a bug: https://issuetracker.google.com/issues/62443122

If you're affected by this issue as well, please star the issue above, so it will get more attention, thanks.

like image 668
fasteque Avatar asked May 18 '17 13:05

fasteque


1 Answers

I've experienced the same issue but updating to support library version 25.4.0 fixed this. Please note that from 25.4.0 you have to include

maven {
    url "https://maven.google.com"
}

in the repositories. See Support Library Setup.

like image 136
Tamás Cseh Avatar answered Sep 30 '22 20:09

Tamás Cseh