如何使用 ViewPager 显示相同的片段但每次加载不同的数据?

How can I use a ViewPager to display the same fragment but loaded with different data each time?

我正在尝试设置一个 ViewPager 以显示相同的模板片段,但在每次滑动后填充不同的数据。我在这里使用虚拟数据,但我的问题是每次滑动后都会加载相同的数据。我到底做错了什么?

这是我的代码:

测试:

public class Test {
    private String a;
    private String b;
    private int c;

    public Test(String a, String b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public static final Test[] testData = {
            new Test("Title 1", "Description 1", 1),
            new Test("Title 2", "Description 2", 2),
            new Test("Title 3", "Description 3", 3),
            new Test("Title 4", "Description 4", 4),
            new Test("Title 5", "Description 5", 5)
    };

    public String getA() {
        return a;
    }

    public String getB() {
        return b;
    }

    public int getC() {
        return c;
    }
}

测试片段:

public class TestFragment extends Fragment {

    private static final String ARG_ID = "Id";
    private int Id;
    private String a;
    private TextView aTV;
    private String b;
    private TextView bTV;
    private int c;
    private TextView cTV;

    public static TestFragment newInstance(int Id) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_ID, Id);

        TestFragment fragment = new TestFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);

        Id = (Integer) getActivity().getIntent().getExtras().get(EXTRA_ID);
        a = Test.testData[Id].getA();
        b = Test.testData[Id].getB();
        c = Test.testData[Id].getC();

        aTV = (TextView) view.findViewById(R.id.a);
        aTV.setText(a);

        bTV = (TextView) view.findViewById(R.id.b);
        bTV.setText(b);

        cTV = (TextView) view.findViewById(R.id.c);
        cTV.setText(String.valueOf(c));

        return view;
    }
}

ViewPagerActivity:

public class ViewPagerActivity extends AppCompatActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        viewPager = (ViewPager) findViewById(R.id.view_pager);

        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                return TestFragment.newInstance(position);
            }

            @Override
            public int getCount() {
                return 5;
            }
        });
    }
}

您没有获得 TestFragment 片段参数 ,其中您有 Id 变量,而是您试图从中获取数据Intent 附加功能:

Id = (Integer) getActivity().getIntent().getExtras().get(EXTRA_ID);

您应该通过这种方式获得您在 TestFragmentnewInstance 方法中提供的参数:

Id = getArguments().getInt(ARG_ID);