我有我的活动一段代码是这样的:从我的recyclerview中删除项目后,如何刷新我的片段?
for(int counter = 0; counter < markers.size(); counter++) {
sum_markers += markers.get(counter).getPrice();
}
所以后,我从我的recyclerview删除一个项目我必须以某种方式更新我的数据,因为我必须重新计算价格的总和(和很多其他的东西)。
我有我的活动一段代码是这样的:从我的recyclerview中删除项目后,如何刷新我的片段?
for(int counter = 0; counter < markers.size(); counter++) {
sum_markers += markers.get(counter).getPrice();
}
所以后,我从我的recyclerview删除一个项目我必须以某种方式更新我的数据,因为我必须重新计算价格的总和(和很多其他的东西)。
使用下面的代码
public void removeItem(List<TodoItem> currentist){
list= currentist;
notifyDataSetChanged();
}
notifyDataSetChanged():它view.so每次刷新里面回收的列表,当你修改的东西用它,并保持在适配器当前列表。
如果你想刷新你的片段后,你删除任何项目,你必须使用下面的代码。
getSupportFragmentManager()
.beginTransaction()
.detach(contentFragment) // detach the current fragment
.attach(contentFragment) // attach with current fragment
.commit();
希望这些帮助你。
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
你可以看看这个refresh recyclerview in a fragment
但如果是使用ListView和自定义适配器可以调用ListAdapter.notifyDataSetChanged()
以强制列表刷新一旦数据发生了变化。
感谢您的回答,但我想刷新我的片段,因为我必须重新计算价格的总和。 –
@mosómaci请检查更新的答案 –
对于从列表中删除项目后更新您的用户界面,只需通过notifyDataSetChanged()通知;在UI线程中调用此适配器。它会自动更新。
为什么需要刷新?我想你想刷新片段右侧显示的列表? –
以防万一您正在考虑刷新片段将刷新您的列表,通过使用 notifyDataSetChanged()刷新您的列表; –
@jiteshmohite这个清单工作完美,我想刷新我的片段,因为我必须重新计算价格总和。 –