如何通过 FragmentPagerAdapter 将变量传递给 Fragment?

How do I pass a variable through a FragmentPagerAdapter to a Fragment?(如何通过 FragmentPagerAdapter 将变量传递给 Fragment?)

本文介绍了如何通过 FragmentPagerAdapter 将变量传递给 Fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个android初学者,正在努力学习,这是我的第一个问题,所以如果问题太简单,请原谅,如果我使用论坛不正确,请告诉我.

I'm an android beginner, trying to learn, and this is my first question, so please excuse me if the question is too simple and please tell me if I'm using the forum incorrectly.

我有一个使用包含 viewpager 的布局的 FragmentActivity;该活动创建了一个 MyFragmentPagerAdapter 实例,该实例创建了几个 MyFragment 实例.都是 support.v4 版本.

I have a FragmentActivity using a layout that includes a viewpager; the activity creates an instance of MyFragmentPagerAdapter, which creates several instances of MyFragment. All are support.v4 versions.

我正在尝试找到一种将整数值(表示用户之前所做的选择,实际上可能是可绘制的 id)传递给片段的方法.我正在使用意图传递它到这个活动,我知道我可以使用包将值从 FragmentPagerAdapter 传递到片段,但是我找不到从活动中获取它到 FragmentPagerAdapter 的方法.我尝试了几种方法,包括更改构造函数,但无法使其正常工作.

I'm trying to find a way to pass an integer value (representing a selection the user has made previously, which could actually be a drawable id) to the fragment. I'm using an intent to pass it to this activity and I know I can can use a bundle to pass a value from the FragmentPagerAdapter to the fragment, but I can't find a way to get it from the activity to the FragmentPagerAdapter. I have tried several ways including changing the constructor but couldn't get that to work.

我错过了一些简单的东西吗?最好的方法是什么?

Am I missing something simple? What is the best way to do this?

    public class SecondActivity extends FragmentActivity {
      private MyIntegerAdapter1 mAdapter1;
      private ViewPager mPager1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        mAdapter1 = new MyIntegerAdapter1(getSupportFragmentManager());
        mPager1 = (ViewPager) findViewById(R.id.pager1);
        mPager1.setAdapter(mAdapter1);

FragmentPagerAdapter

    public class MyIntegerAdapter1 extends FragmentPagerAdapter {
    public MyIntegerAdapter1(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            Fragment f1 = new IntegerFragment();
            Bundle args1 = new Bundle();
            args1.putInt("param", R.drawable.image1);
            args1.putInt("number", 1);
            f1.setArguments(args1);
            return f1;
    etc

片段

    public class IntegerFragment extends Fragment {
      private int imageResourceId;
      private int numberSelected;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //change to avoid orientation crash
        imageResourceId = getArguments().getInt("param");
        numberSelected = getArguments().getInt("number");

推荐答案

问题结束.我发现(从下面的链接)片段如何在其活动中调用方法.该方法正在提供价值.我不确定这是不是最好的方法,但它很简单而且很有效.

Problem over. I found out (from the link below) how a fragment can call a method in its activity. That method is providing the value. I'm not sure it's the best way but it's simple and it works.

在片段及其容器活动之间传递数据

这篇关于如何通过 FragmentPagerAdapter 将变量传递给 Fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何通过 FragmentPagerAdapter 将变量传递给 Fragment?

基础教程推荐