I am running into an issue that isn't new. There are many threads about it saying that I need to update it from the main thread.
I tried this:
How do I make RecyclerView update its layout?
but not really working. I suspect I am not calling it from the main thread. But I don't know yet. I am using a FragmentStatePagerAdapter, here's the stripped version of the code (kind of long unfortunately):
public Fragment getItem(int position) {
BaseFragment tabFragment;
switch (position) {
default:
case 0:
tabFragment = MyTabFragment.newInstance();
break;
case 1:
tabFragment = MyTabFragment.newInstance();
break;
}
Each tab has a recyclerview with a list of items. now I have in my tab fragment I have layout a button that fires a custom dialog. If the user clicks OK, it must refresh the tabs behind the dialog, but unfortunately it never does. The dialog goes away and my recycler view behind the tabs stay the same :(
here is the stripped version of the dialog with a btnlistener:
public class MyTabFragment extends Fragment
{
//... a bunch of code
private void setBtnListener(Button followBtn) {
followBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
if (!myData.isOK()) {
final MyCustomDialog dialog = new MyCustomDialog(view.getContext(), myData, null);
dialog.setListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// up to this point the code works.
OnTestClick click = new OnTestClick(myData, null);
click.onClick(view);
dialog.dismiss();
// now the dialog goes away and I must refresh the tabs behind the dialog
// this tries to do it, but it does not work!
fetchMessages();
}
});
dialog.show();
}
}
});
}
}
if you read it so far, you are pretty patient :)
now is where I attempt to refresh the tabs:
public class MyTabFragment extends Fragment
{
//... a bunch of code
void fetchMessages() {
mMessagesTask = new MyMessagesTask(getAppContext(), mMessageType);
mMessagesTask.setCallback(new MyMessagesTask.OnResult(){
@Override
public void onFinished() {
setIsLoading(false);
if (mList != null) {
MyMessagesRecyclerAdapter adapter = ((MyMessagesRecyclerAdapter) mList.getAdapter());
// it gets here, but the recycle view never refreshes :(
adapter.notifyDataSetChanged();
Log.d("[test]", "callback called but the tabs still show old messages");
}
}
});
mMessagesTask.execute(mVenue);
}
}
finally my custom task
public class MyMessagesTask extends AsyncTask<MyData, Void, Void> {
//... a bunch of code
private OnResult mCallback;
public void setCallback(OnResult mCallback) {
this.mCallback = mCallback;
}
public interface OnResult {
void onFinished();
}
@Override
protected void onPostExecute(Void param) {
super.onPostExecute(param);
if (mCallback != null) {
mCallback.onFinished();
}
}
//... a bunch of code
}
There is a bunch of stuff I did not include, but hopefully someone can spot the issue.
thx!