Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
During my work I faced a serious problem: flash effect when refreshing items of the BrowseFragmentās rows.
source: https://developer.android.com/training/tv/playback/browse.htmlIntroduction
Before talking about the solution of the problem, Letās introduce the BrowseFragment. The BrowseFragment is a fragment for creating media catalog. In the media catalog, we can browse categories from the left, and select contents of the selected category on the right. The media catalog is composed of a RowsFragment and a HeadersFragment. A BrowseFragment renders the elements of its ObjectAdapter as a set of rows in a verticalĀ list.
Now letās imagine we want to refresh every 5 seconds the data of the RowsFragment with a background task.How can we do thatĀ ?
First solution: just update theĀ adapter
The BrowseFragment use an ArrayObjectAdapter to display data. We just need to:1. clear the items of the adapter (clear)2. add new items to the adapter(addAll)Problem: the flashĀ effect
Every time there is a refresh, the fragment blinks. The flash effect is caused by the adapter notifying the UI for each operation on the items. As we can see the method clear() and addAll() have a notify method of ArrayObjectAdapter have a notifyĀ method:
public void clear() { int itemCount = mItems.size(); if (itemCount == 0) { return; } mItems.clear(); notifyItemRangeRemoved(0, itemCount); //notifies UI}
public void addAll(int index, Collection items) { int itemsCount = items.size(); if (itemsCount == 0) { return; } mItems.addAll(index, items);notifyItemRangeInserted(index, itemsCount);//notifies UI}
Second solution: your ownĀ adapter:
we can create our own adapter and choose when and how to notify the changes on theĀ items.
I created a custom ObjectAdapter with a new replaceAll() method.
public void replaceAll(Collection items){ int itemsCount = items.size(); if (itemsCount == 0){ return; } mItems.clear(); mItems.addAll(index, items); notifyItemRangeChanged(0, itemsCount);}
Show me theĀ code
The full class isĀ here:
References:
Creating a Catalog Browser | Android Developers
N E X T ā Documenting My Android Adventure
Before you goā¦ If you enjoyed this post, you will love to subscribe to my newsletter. Get my cheat sheet: āAndroid Studio keyboard shortcuts cheatĀ sheetā.
How to refresh the Android Tv BrowseFragment ? was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.