我想在浏览片段中重新加载一些行的数据。基本上,我想在不引起浏览片段闪烁效果的情况下重置适配器数据。有什么想法吗?类似于列表视图中的
notifyDataSetChanged()
。谢谢。notifyDataSetChanged()
。谢谢。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 leanback 的列表行中
我将从那里复制我的答案,希望这能有所帮助。
要向 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));
public void refreshUI() {
prepareBackgroundManager();
setupUIElements();
loadRows(); //Generate and populate all the rows
setupEventListeners();
}
您可以创建自己的适配器,并添加一个新的replaceAll()方法。
public void replaceAll(Collection items){
int itemsCount = items.size();
if (itemsCount == 0){
return;
}
mItems.clear();
mItems.addAll(index, items);
notifyItemRangeChanged(0, itemsCount);
}
完整源代码:link
/**
* 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);
}
通知一系列项目的内容已更改。请注意,这与添加或删除项目不同。
如果您想添加或删除项目,请尝试 this:
1.mRowsAdapter.clear();
2.mRowsApdapter.addAll();
replaceAll
,我使用它来初始化列表并逐步更新。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()
}
}