简体   繁体   English

如何使用 viewPager 和 Fragments 传递多个数据

[英]How to use viewPager and Fragments for passing multiple data

Im trying to pass string data together with images .我试图将string数据与images一起传递。 Why when running this code I get only image 'nature4' shown but there is no 'nature2' image and strings 'Fragment1' etc..为什么在运行此代码时,我只显示image “nature4”,但没有“nature2” imagestrings “Fragment1”等。

 public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;

    private ArrayList<String> strs;

    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("x","onCreateActivity");

        strs = new ArrayList<String>();
        strs.add("Fragment 1");
        strs.add("Fragment 2");
        strs.add("Fragment 3");
        strs.add("Fragment 4");
        strs.add("Fragment 5");

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

        adapter = new FragmentPagerAdapter(getSupportFragmentManager(), strs);
        viewPager.setAdapter(adapter);
    }

    public class FragmentPagerAdapter extends FragmentStatePagerAdapter {

        private ArrayList<String> itemData;

        public FragmentPagerAdapter(FragmentManager fm, ArrayList<String> itemData) {
            super(fm);
            this.itemData = itemData;
        }

        @Override
        public int getCount()
        {
            return itemData.size();
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            Log.i("x","destroyItem");
        }
        @Override
        public Fragment getItem(int position){
        Log.i("x","getItem");
            // pass data to fragment
            Fragment1 f = Fragment1.newInstance(itemData.get(position));
            return f;

        }
    }
}

And Fragment.class和 Fragment.class

 public class Fragment1 extends Fragment {

    public  Fragment1(){}

    String stringValue;

    private int [] images = {R.drawable.nature2,R.drawable.nature4};

    public  static Fragment1 newInstance(String str)
    {
        Fragment1 slider=new Fragment1();
        Bundle b=new Bundle();
        b.putString("stringValue", str);
        slider.setArguments(b);
        return slider;
    }

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("x","onCreateViewFragment");

        View view = inflater.inflate(R.layout.fragment1, container, false);
        textView = (TextView) view.findViewById(R.id.textView);
        ImageView photo = (ImageView) view.findViewById(image);
        photo.setImageResource(images[1]);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i("x","onActivityCreatedFragment");
        super.onActivityCreated(savedInstanceState);
        stringValue = getArguments().getString("stringValue");
        textView.setText(stringValue);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i("x","onCreateFragment");
        super.onCreate(savedInstanceState);

    }
}

I think you need to make your question clear.我认为你需要把你的问题说清楚。 If you want to pass String stringValue and in imageResId from Activity to Fragment , try the following codes.如果要将String stringValuein imageResIdActivity传递到Fragment ,请尝试以下代码。

public static Fragment1 newInstance(String str, int imageResId) {
    Fragment1 slider = new Fragment1();
    Bundle b=new Bundle();
    b.putString("stringValue", str);
    b.putInt("image_resid", imageResId);
    slider.setArguments(b);
    return slider;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    stringValue = getArguments().getString("stringValue");
    imageResId = getArguments().getIn("image_resid");
  }

FragmentPagerAdapter: FragmentPagerAdapter:

 public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
        ...
    @Override
    public Fragment getItem(int position){
      Fragment fragment;
      String str = itemData.get(position);
      int resId;
      if (position == 0) {
        resId = R.drawable.nature2;
      } else if (position == 1) {
        resId = R.drawable.nature4;
      }
      fragment = Fragment1.newInstance(str, resId);
      ...

      return fragment;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM