0

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!

Community
  • 1
  • 1
gmmo
  • 2,577
  • 3
  • 30
  • 56
  • Do you want to update layout without any data changes ? – Arkar Aung Feb 05 '16 at 04:29
  • Are you adding the items to the mList ? – Sunil Sunny Feb 05 '16 at 05:05
  • update the list, then call the notifyDataSetChanged() on adapter. – Saritha G Feb 05 '16 at 06:39
  • I tried to update the data, but still no refresh. Is there a way to refresh the entire layout? – gmmo Feb 05 '16 at 17:39
  • What you mean with _I tried to update the data_? For having a layout update according to what's inside items list it's important that your fragment and your adapter share the same list instance or the adapter list instance is aligned with the one owned by fragment. From your code it's not clear how `MyMessagesRecyclerAdapter` is initialized. – andrea.petreri Feb 07 '16 at 08:48

1 Answers1

0

You need to change in Layout using Fragment object if in case of fragments if you can pass ArrayList then notifydatasetChange is working.In case of fragments it is not posible to make changes because every time when uoy notify the adapter it will send a new object

Rohit Heera
  • 2,709
  • 2
  • 21
  • 31