Android Fragments Recycler视图在返回片段后不刷新

eqqqjvef  于 2022-11-24  发布在  Android
关注(0)|答案(2)|浏览(235)

我有收藏夹片段与recyclerview,数据显示良好,但当我点击一个reyclerview项目,并将其从收藏夹中删除时,按回收藏夹片段仍显示在recylerview中,我知道问题是recyclerview不是refriging,因为当我重新启动应用程序的项目被删除,我尝试notifydatasetchanged但它不工作在这里是在创建视图和收藏夹片段的恢复

@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();

}
cig3rfwq

cig3rfwq1#

您可以尝试以下方法更新您的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);
}

已添加

您可以使用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();
    }
6yoyoihd

6yoyoihd2#

override fun onResume() {
    super.onResume()
    binding.mRecyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
}

相关问题