-2

So, I'm making a screen with Leanback's Browse Fragment and CardPresenter.

Inside my fragment that extends BrowseFragment, I have a method for drawing the UI:

    private void loadCardRows() {
    mRowsAdapter = new CustomArrayObjectAdapter(new ListRowPresenter());

    final List<UiType> uiTypeList = new ArrayList<>(uiTypes);

    for (UiType uiType : uiTypeList) {
        HeaderItem cardPresenterHeader = new HeaderItem(0, uiType.getName());
        List<TypeReportItem> items = performUiTypeFiltering(uiType.getEndpointType());
        CardPresenter cardPresenter = new CardPresenter(attributesHelper);
        CustomArrayObjectAdapter cardRowAdapter = new CustomArrayObjectAdapter(cardPresenter);

        for (TypeReportItem item : items) {
            cardRowAdapter.add(item);
        }

        mRowsAdapter.add(new ListRow(cardPresenterHeader, cardRowAdapter));

    }
    setAdapter(mRowsAdapter);
}

Now I'm having a service that loads some data every few seconds. That data is reachable through attributesHelper that I'm passing to CardPresenter.. How am I supposed to reload that data without causing the screen to blink every few seconds?

joe
  • 1,341
  • 4
  • 21
  • 32

3 Answers3

3
mRowAdapter.notifyArrayItemRangeChanged(startingIndex, mRowAdapter.size());

Starting index is the one from which position your are updating data.Dont give starting index something like 0,which will result in screen blink from index 0

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Thank you, but the problem with this is, the screen blinks every few seconds which doesn't look nice :/ – joe Sep 19 '17 at 09:24
  • Thats what..You will see the screen blinking when you call the range changed for every item..so while giving the starting index specify the position +1 after your visible data – Sharath kumar Sep 19 '17 at 09:27
  • Oh.. yeah, I set index to 0. But how can I get the position since this ArrayObjectAdapter doesn't provide any method for getting position? – joe Sep 19 '17 at 09:37
  • I am saying like row position.Say example if you are updating first row by fetching data maintain a counter globally...and keep the value updated..Use that value to startingindex..Hope it works – Sharath kumar Sep 19 '17 at 09:40
  • But if I want to update the whole list? Starting index should be 0, right? – joe Sep 19 '17 at 10:49
  • ya..when you want to update from some row without effecting upper rows use some specific position.If you want to update all rows use 0. – Sharath kumar Sep 19 '17 at 11:03
  • I should update all the rows, but without this blink efect.. it shouldn't be redrawing the whole UI again... – joe Sep 19 '17 at 11:49
1

Maybe this anwser will be helpful

for (int i = 0; i < mAdapter.size(); i++) {
ListRow listRow = ((ListRow) mAdapter.get(i));
ArrayObjectAdapter listRowAdapter = ((ArrayObjectAdapter) listRow.getAdapter());
if (listRowAdapter.size() > 0) {
    listRowAdapter.notifyArrayItemRangeChanged(0, listRowAdapter.size());
}
}

android tv -Reloading adapter data

I have used this code in my project. No blink happened.

Tracyliu
  • 11
  • 3
0
notifyArrayItemRangeChanged

caused blinking, so try this Adapter

class RefreshableArrayObjectAdapter(presenterSelector: PresenterSelector) :
    ArrayObjectAdapter(presenterSelector) {
    fun refresh() {
        notifyChanged()
    }
}
Danylo.Vus
  • 969
  • 9
  • 11