0

I have a situation when I have large set of data in firebase database and I want to show those data in ViewPager with Fragments. The database and callbacks works fine but it clogging up the main thread when I try to add it to ViewPager and call notifyDataSetChanged().

Im using EventBus for communication between the Activity and Singleton class to handle add firebase related taks.

Here are some code snippets

Singleton Class

databaseProjectsRef.addChildEventListener(new ChildEventListener() {
        public void onChildAdded(DataSnapshot snapshot, String s) {
            // Get the project from the snapshot and add it to the UI
            Project project = snapshot.getValue(Project.class);
            project._id = snapshot.getKey();
            add(project, ProjectType.NORMAL);
        }
        ...
    });


private void add(Project project, ProjectType projectType) {
    if (!data.contains(project)) {
        project.projectType = projectType;
        data.add(project);
        EventBus.getDefault().post(new RefreshDataEvent());
    }
}

MainActivity

@Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshDataEvent event) {
    if (mAdaptor != null) {
        mAdaptor.setData();
    }
}

Adaptor

public class ProjectAdaptor extends FragmentStatePagerAdapter {
private final EmptyInterface emptyInterface;
private List<Project> mData;

public ProjectAdaptor(FragmentManager fm, EmptyInterface emptyInterface) {
    super(fm);
    mData = new ArrayList<>();
    this.emptyInterface = emptyInterface;
}

@Override
public Fragment getItem(int position) {
    PagerFragment item = PagerFragment.newInstance(mData.get(position));
    return item;
}

@Override
public int getCount() {
    return mData.size();
}

public void setData() {
    mData = ProjectContext.getInstance().getData();
    emptyInterface.isEmpty(mData.isEmpty());
    notifyDataSetChanged();
}

public interface EmptyInterface {
    void isEmpty(boolean isEmpty);
}

Any help would be appreciated. The most relevant question i could find was this but with no answers.

Community
  • 1
  • 1
Ritesh Shakya
  • 575
  • 5
  • 17

0 Answers0