Recyclerview在返回片段后没有刷新

时间:2015-11-03 21:47:51

标签: android android-fragments android-recyclerview

我有收藏夹片段与recyclerview,数据显示良好 但是当我点击一个reyclerview项目并从收藏夹中删除它时,回到收藏夹片段仍然显示在recylerview中,我知道问题是recyclerview不重新填充,因为当我重新启动应用程序时项目被删除,我试过notifydatasetchange但它不起作用 这里是oncreateview和onresume的收藏夹片段

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView= inflater.inflate(R.layout.fragment_favourites, container, false);

    activity=getActivity();

    tinydb = new TinyDB(activity);

    liststorieshits=(RecyclerView) rootView.findViewById(R.id.lsv_cat_item_fav);
    liststorieshits.setLayoutManager(new LinearLayoutManager(getActivity()));





    rippleView = ((RippleView) rootView.findViewById(R.id.rect_fav));
    liststorieshits.setHasFixedSize(true);
    liststorieshits.addItemDecoration(new VerticalDividerItemDecoration.Builder(getActivity()).build());
    mAdapter=new FavoritesAdapter(getActivity(),itemsList);

    liststorieshits.setAdapter(mAdapter);

    progress = new ProgressDialog(getActivity());
    progress.setTitle("Loading");
    progress.setMessage("Wait while loading Stories");
    progress.show();

    RestAdapter restAdapter=new RestAdapter.Builder().setEndpoint(getString(R.string.categories_feed)).build();
    final CategoriesApiInterface categoriesApiInterface=restAdapter.create(CategoriesApiInterface.class);



    idlist2=tinydb.getListInt("strid");
    int ab=0;
    for(int i=0;i<idlist2.size();i++){
        ab=idlist2.get(i);
        categoriesApiInterface.getStreams3(ab, new Callback<List<Stories>>() {
            @Override
            public void success(List<Stories> stories, Response response) {

                progress.dismiss();
                if (stories == null || stories.isEmpty()) {
                    return;
                }

                for (Stories stories1 : stories) {
                    itemsList.add(stories1);


                }
                mAdapter.notifyDataSetChanged();
                Log.e("Stories", "krb :" + itemsList.size());
            }

            @Override
            public void failure(RetrofitError error) {
                progress.dismiss();

                Log.e("Stories", "Retrofit error " + error.getMessage());
            }
        });

    }
    return rootView;

}
@Override
public void onResume() {
    super.onResume();
    mAdapter.notifyDataSetChanged();

}

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式来更新您的RecycleView:

    @Override
    public void onResume() {
    super.onResume();

    itemsList.clear();
    idlist2=tinydb.getListInt("strid");
    int ab=0;
    for(int i=0;i<idlist2.size();i++){
        ab=idlist2.get(i);
        categoriesApiInterface.getStreams3(ab, new Callback<List<Stories>>() {
            @Override
            public void success(List<Stories> stories, Response response) {

                progress.dismiss();
                if (stories == null || stories.isEmpty()) {
                    return;
                }

                for (Stories stories1 : stories) {
                    itemsList.add(stories1);


                }
                mAdapter.notifyDataSetChanged();
                Log.e("Stories", "krb :" + itemsList.size());
            }

            @Override
            public void failure(RetrofitError error) {
                progress.dismiss();

                Log.e("Stories", "Retrofit error " + error.getMessage());
            }
        });

    }

    mAdapter = new FavoritesAdapter(getActivity(),itemsList);
    liststorieshits.setAdapter(mAdapter);
}

<强> ADDED

您可以使用update()更新单独功能中的数据,如下所示:

private void update() {
        itemsList.clear();
        idlist2=tinydb.getListInt("strid");
        int ab=0;
        for(int i=0;i<idlist2.size();i++){
            ab=idlist2.get(i);
            categoriesApiInterface.getStreams3(ab, new Callback<List<Stories>>() {
                @Override
                public void success(List<Stories> stories, Response response) {

                progress.dismiss();
                if (stories == null || stories.isEmpty()) {
                    return;
                }

                for (Stories stories1 : stories) {
                    itemsList.add(stories1);


                }
                mAdapter.notifyDataSetChanged();
                Log.e("Stories", "krb :" + itemsList.size());
            }

            @Override
            public void failure(RetrofitError error) {
                progress.dismiss();

                Log.e("Stories", "Retrofit error " + error.getMessage());
            }
        });

    }

    mAdapter = new FavoritesAdapter(getActivity(),itemsList);
    liststorieshits.setAdapter(mAdapter);
}

并使用它:

    @Override
    public void onResume() {
        super.onResume();
        update();
    }