Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use single Fragment for all the pages in the ViewPager?

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.

like image 559
Sridhar Avatar asked Oct 21 '22 15:10

Sridhar


1 Answers

Now my problem is all the pages holds first 3 student details.

If this is happening, most likely your displayStudentDetails() method only gets the first three student details that you keep seeing and doesn't take in consideration the position(and the student details that come with that position) of the Fragment in the ViewPager. As you didn't posted the method I can't recommend a solution.

I have maintained a common ArrayList object which holds all the student objects.

Where did you do this and how do you store that list?

f = StudentPageFragment.newInstance(_context);

Please don't pass the Context to your fragments as the Fragment class has a reference to the Context/Activity through the getActivity() method that you should use instead.

You should build the Fragments like this:

@Override
public Fragment getItem(int position) {
    return StudentPageFragment.newInstance(position);
}

where the newInstance() method will be:

public static Fragment newInstance(int position) {
      StudentPageFragment f = new StudentPageFragment();
      Bundle args = new Bundle();
      args.putInt("position", position);
      f.setArguments(args); 
      return f;
}

You'll then retrieve the position and use it in the Fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.stud_list_page,
            container, false);
    // initialize all widgets here        
    displayStudentDetails(getArguments().getInt("position"));
    return root;
}

In the displayStudentPosition you could get the values like this:

protected void displayStudentDetails(int position) {
        ArrayList<Student>studList = User.getStudentsList();
        if (studList != null) {
        for (int i = position; i < 3 && i < studList.size; i++) {
             // populate data
        } 
        } 
}
like image 88
user Avatar answered Oct 27 '22 11:10

user