In my application I have to show the Student details in ViewPager
. I used one fragment (say StudentPageFragment
) and I write widget initializing code in onCreateView()
like:
public static Fragment newInstance(Context context) {
StudentPageFragment f = new StudentPageFragment();
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.stud_list_page,
null);
// initialize all widgets here
displayStudentDetails();
return root;
}
protected void displayStudentDetails() {
ArrayList<Student>studList = User.getStudentsList();
if (studList != null) {
int loc = (pageIndex * 3);
for (int i = 0; i < 3; i++) {
if (loc < studList.size()) {
// populate data in view
}
loc++;
}
}
}
I have maintained a common ArrayList<Student>
object which holds all the student objects.
And In displayStudentDetails()
methods, I populate the first 3 Student objects there. If we swipe next page the same fragment should called the displayed next 3 Student objects.
And in ViewPagerAdapter
class:
@Override
public Fragment getItem(int position) {
Fragment f = new Fragment();
f = StudentPageFragment.newInstance(_context);
StudentPageFragment.setPageIndex(position);
return f;
}
@Override
public int getCount() {
return User.getPageCount();// this will give student list size divided by 3
}
Now my problem is all the pages holds first 3 student details. Please provide me the best way to do this.