从我的recyclerview中删除项目后,如何刷新我的片段?

时间:2017-04-11 15:05:03

标签: android android-recyclerview

我的活动中有一段代码如下:

for(int counter = 0; counter < markers.size(); counter++) {
        sum_markers += markers.get(counter).getPrice();
    }

因此,在我从recyclerview中删除了一个项目之后,我必须以某种方式更新我的数据,因为我必须重新计算价格总和(以及许多其他内容)。

3 个答案:

答案 0 :(得分:2)

// 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()强制刷新列表。

答案 1 :(得分:2)

使用以下代码

public void removeItem(List< TodoItem > currentist){
        list= currentist;
        notifyDataSetChanged();    
    }

notifyDataSetChanged():每次修改使用它的内容并将当前列表保存在适配器中时,它会刷新recycleler view.so中的列表。

如果您想在删除任何项目后刷新片段,则必须使用以下代码。

getSupportFragmentManager()
                .beginTransaction()
                .detach(contentFragment) // detach the current fragment
                .attach(contentFragment) // attach with current fragment
                .commit();

希望这些可以帮助你。

答案 2 :(得分:0)

要在从列表中删除项目后更新UI,只需通过notifyDataSetChanged()通知;在UI线程上的适配器中调用它。它会自动更新。

相关问题