Thursday, August 4, 2016

A Simple Solution to Manage OffScreen Fragments in Pager Adapter

For a while I've been facing an issue with the Fragments that I used in one of my project. Here's the scenario described in points-
1. I've a total of 3 Fragments in my View Pager.
2. First two Fragments comprises of a Table with uncommon number of columns. First Fragment has a table with 2 columns and Second Fragment has a table with 3 columns. I did the code to modify the columns dynamically. Also the header Row is a static table row inside the layout file with three child views inside it.
3. I've inflated same layout for both of the Fragments(1st and 2nd) as most of the content is same.
4. When initially the activity is launched first fragment is shown perfectly with two columns. It's also fine when I swiped once to second fragment, the table showed 3 columns according to the requirement. Problem occurred when I swiped to the third fragment and comes back to the first fragment. It showed the header row as modified in second fragment.
5. I played with many methods of Fragment, but didn't succeeded.
I came to know about a new method added to View Pager in Revision4 of Support Package -
Setting the limit to 2 means that when initially the fragment first view is shown the next two views will also get active with the first fragment.

No comments:

Post a Comment