简体   繁体   English

如何更新片段中的 recyclerview 列表? (notifyDataSetChanged 不起作用)

[英]How do I update the recyclerview list in the fragment? (notifyDataSetChanged not working)

There is a fragment in my project, when you click the recyclerview there, it goes to that item (detail activity opens).我的项目中有一个片段,当您单击那里的 recyclerview 时,它会转到该项目(详细活动打开)。 There is a like button in detail activity and when you press it, in the upper layer;详细活动中有一个like按钮,当你按下它时,在上层; The recyclerview in fragment needs updating but I couldn't.片段中的 recyclerview 需要更新,但我不能。

在此处输入图像描述

MY CODE: (fragment)我的代码:(片段)

    RecyclerView savedRecyclerView;
    SQLiteHandler sqLiteHandler;
    SavedAdapter savedAdapter;
    List<SavedModel>  itemModels = new ArrayList<>();
    @SuppressLint("StaticFieldLeak")
    public static SavedFragment instance;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_saved, container, false);
        instance = this;


        savedRecyclerView = view.findViewById(R.id.savedRecyclerView);


        sqLiteHandler = new SQLiteHandler(getActivity());

        itemModels = sqLiteHandler.getSaved();


        LinearLayoutManager mainLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        savedRecyclerView.setLayoutManager(mainLayoutManager);

        savedAdapter = new SavedAdapter(getContext(), itemModels, "diyet");
        savedRecyclerView.setAdapter(savedAdapter);
        savedAdapter.notifyDataSetChanged();

        return view;
    }

    public static SavedFragment getInstance() {
        return instance;
    }

   public void refreshFragment() {
        Log.e("TAG_FRAGMENT", "refresh");
        itemModels.clear();
        itemModels = sqLiteHandler.getSaved();
        if (itemModels.isEmpty()) {
            empty_animation.setVisibility(View.VISIBLE);
        } else {
            LinearLayoutManager mainLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
            savedRecyclerView.setLayoutManager(mainLayoutManager);
            savedAdapter = new SavedAdapter(getContext(), itemModels, "diyet");
            savedRecyclerView.setAdapter(savedAdapter);
            savedAdapter.notifyDataSetChanged();
        }
   
getFragmentManager().beginTransaction().detach(this).attach(this).commit();

     getActivity().getSupportFragmentManager().beginTransaction().detach(this).attach(this).commit();

    }


I try this but not working我试试这个但不工作

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

Detail Activity UnFav Button Click详细活动 UnFav 按钮单击



if (SavedFragment.getInstance()!=null){
   SavedFragment.getInstance().refreshFragment();
   Log.e("TAG_FRAGMENT","savednotnull");
}

           

Use this in your onResume() :在你的onResume()中使用它:

savedAdapter.notifyItemRangeChanged(0, itemModels.size());

like this:像这样:

  @Override
   public void onResume() {
        super.onResume();
        savedAdapter.notifyItemRangeChanged(0, itemModels.size());
   }

Change in only that item which is click in itemModels list and than do:仅更改在 itemModels 列表中单击的那个项目,而不是:

savedAdapter.notifyDataSetChanged();
public void refreshFragment() {           
    getFragmentManager().beginTransaction().detach(this).attach(this).commit();
}

notifyDataSetChanged must be run on the UI Thread, if it doesn't you should force it. notifyDataSetChanged必须在 UI 线程上运行,如果不是,你应该强制它。 Try this where you want to update recyclerview after changing the data, it worked for me when others don't.在更改数据后要更新recyclerview的地方试试这个,当其他人不这样做时,它对我有用。

((Activity) getContext()).runOnUiThread(new Runnable() {
        @Override
        public void run() {
            yourAdapter.notifyDataSetChanged();
        }
    });

If it doesn't work, maybe you should review your data, adapter, and recyclerview.如果它不起作用,也许你应该检查你的数据、适配器和recyclerview。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新片段中的 recyclerView - How do I update a recyclerView in a fragment Android 如何在 ThreadPoolExecutor 完成后调用 RecyclerVIew notifyDataSetChanged()? - Android How Do I Call RecyclerVIew notifyDataSetChanged() after ThreadPoolExecutor finishes? 单击添加和删除按钮后,我试图在recyclerview中更新textview。 notifyDataSetChanged(); 方法不起作用 - I'm trying to update the textview in the recyclerview after clicking add and delete button. notifyDataSetChanged(); method is not working RecyclerView notifydatasetchanged 未从片段调用 - RecyclerView notifydatasetchanged not invoked from fragment recyclerview notifyDataSetChanged() 不工作 - recyclerview notifyDataSetChanged() is not working notifyDataSetChanged在片段上无法正常工作 - notifyDataSetChanged not working properly on fragment 在活动/片段中确认警报对话框后,如何在单击时更新我的​​ RecyclerView 按钮状态? - How do I update my RecyclerView button state on click after alert dialog confirmation in activity/fragment? 尝试使用 notifyDataSetChanged 更新我的 listView 自定义列表适配器但不工作 - Trying to update my custom list adapter of listView using notifyDataSetChanged but not working 检测片段中 Recyclerview 上的上下滑动我该怎么做? - Detecting up and down swipes on a Recyclerview in a fragment how can I do that? 如何让notifyDatasetChanged()与ListAdapter一起使用? - How do I get notifyDatasetChanged() to work with a ListAdapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM