There is a fragment in my project, when you click the recyclerview there, it goes to that item (detail activity opens). There is a like button in detail activity and when you press it, in the upper layer; The recyclerview in fragment needs updating but I couldn't.
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
if (SavedFragment.getInstance()!=null){
SavedFragment.getInstance().refreshFragment();
Log.e("TAG_FRAGMENT","savednotnull");
}
Use this in your 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:
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. Try this where you want to update recyclerview after changing the data, it worked for me when others don't.
((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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.