Frage

I've encountered a problem with cwac-pager, and I can reproduce the problem in the PagerDemo demo project.

Steps to reproduce the issue:

  1. Run the PagerDemo project (it initializes 10 EditorFragment pages)
  2. Delete pages 2-8 (nav to page 2 and press Delete until you're left on Editor #9)
  3. You now have 3 fragments: Editor #1, Editor #9, and Editor #10. Swipe left to navigate back to Editor #1.
  4. Press + to add pages. It starts by adding Editor #4 (evidently because there were 3 pages left after our deletes). Add pages until you've added up to Editor #10 (visible to the left of the current page).
  5. Swipe right to Editor #9. The view does not load (there is no edit text for you to edit). Editor #10 (on the right) is the same. Swipe left to the new Editor #9 or Editor #10 to the left of Editor #1, and those views have loaded properly.

If you inspect entries in ArrayPagerAdapter at this time, you'll see that it has the correct number of PageDescriptors, but some of the PageDescriptors have the same tag, e.g. "editor9" and "editor10" appear twice. It seems like the fragments corresponding to these PageDescriptors do not load.

The code in the demo project (and similarly in my project) for building the tags simply concatenates a string and the position of the fragment being created:

private String buildTag(int position) {
    return("editor" + String.valueOf(position));
}

As a workaround I modified buildTag to build a random tag:

private String buildTag(int position) {
    return("page" + UUID.randomUUID().toString());
}

I'm not sure whether this workaround might have unintended negative consequences?

War es hilfreich?

Lösung

You are correct that this is a bug in the demo app, and ArrayPagerAdapter could use some defensive programming here to detect this case and raise an exception. I will work on fixing both. The PageDescriptor needs a unique tag, and my current demo app algorithm won't guarantee that, as you discovered. I have filed an issue for this.

Off the top of my head, a UUID should be OK, though I'll probably just use a monotonically-increasing integer counter.

Thanks for pointing this out!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top