如何在ViewPager中的所有页面上使用单个Fragment? [英] How to use single Fragment for all the pages in the ViewPager?

查看:262
本文介绍了如何在ViewPager中的所有页面上使用单个Fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须在ViewPager中显示学生详细信息.我使用了一个片段(例如StudentPageFragment),并在onCreateView()中编写了小部件初始化代码,例如:

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++;
        }
    }
}

我维护了一个通用的ArrayList<Student>对象,该对象包含所有学生对象.

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

然后在displayStudentDetails()方法中,我在那里填充前3个Student对象.如果我们滑动下一页,则相同的片段应称为显示的下一个3个Student对象.

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.

ViewPagerAdapter类中:

@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
}

现在我的问题是所有页面都包含前3个学生详细信息.请为我提供执行此操作的最佳方法.

Now my problem is all the pages holds first 3 student details. Please provide me the best way to do this.

推荐答案

现在我的问题是所有页面都包含前3个学生详细信息.

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

如果发生这种情况,您的displayStudentDetails()方法很可能只会获取您一直看到的前三个学生详细信息,而没有考虑ViewPager中的>.您未发布该方法,所以我不推荐解决方案.

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.

我维护了一个通用的ArrayList对象,该对象包含所有 学生反对.

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);

f = StudentPageFragment.newInstance(_context);

请不要将Context传递给片段,因为Fragment类通过应使用的getActivity()方法引用了Context/Activity.

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);
}

其中newInstance()方法将是:

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

然后您将检索position并在Fragment中使用它:

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;
}

displayStudentPosition中,您可以得到如下所示的值:

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
        } 
        } 
}

这篇关于如何在ViewPager中的所有页面上使用单个Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆