[英]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.