问题
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.
回答1:
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
}
}
}
来源:https://stackoverflow.com/questions/15195000/how-to-use-single-fragment-for-all-the-pages-in-the-viewpager