问题
I want to re-load some of the rows data in a browse fragment.
Basically I want to reset the adapter data without causing a flash like effect in the browse fragment. Any idea how it can be done?
Something like notifyDataSetChanged()
in list view.
Thanx
回答1:
This will refresh data without losing current position:
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());
}
}
回答2:
You can create your own adapter and add 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);
}
complete source:link
回答3:
I answered something similar in this question: Dynamically add more cards in a list Row android TV leanback
I'll copy my answer from there. Hopefully this helps.
To add items to a presenter;
CardPresenter channelCardPresenter = new CardPresenter();
ArrayObjectAdapter channelRowAdapter = new ArrayObjectAdapter(channelCardPresenter);
channelRowAdapter.add(new Movie("Movie Title"));
HeaderItem header = new HeaderItem(0, "My Channels");
mRowsAdapter.add(new ListRow(header, channelRowAdapter));
Of course this may only work for the first time you're creating the UI. Future times may not do this. In my app, CumulusTV, I create a method that will be called each time I want to do a full redraw of the app:
public void refreshUI() {
prepareBackgroundManager();
setupUIElements();
loadRows(); //Generate and populate all the rows
setupEventListeners();
}
回答4:
Here's my kotlin version of @ewilly's answer, implementing only replaceAll
which I use to both initially populate and incrementally update the list.
class RefreshableArrayObjectAdapter(presenter: Presenter): ObjectAdapter(presenter) {
var items: ArrayList<Any> = arrayListOf()
override fun isImmediateNotifySupported(): Boolean = true
override fun size(): Int = items.size
override fun get(position: Int): Any = items[position]
fun replaceAll(list: Collection<Any>) {
items.clear()
items.addAll(0, list)
notifyChanged()
}
}
回答5:
/**
* Notify that the content of a range of items changed. Note that this is
* not same as items being added or removed.
*
* @param positionStart The position of first item that has changed.
* @param itemCount The count of how many items have changed.
*/
public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
notifyItemRangeChanged(positionStart, itemCount);
}
Notify that the content of a range of items changed. Note that this is * not same as items being added or removed.
if you want add or remove item,please try this:
1.mRowsAdapter.clear();
2.mRowsApdapter.addAll();
来源:https://stackoverflow.com/questions/33870065/android-tv-reloading-adapter-data