将ViewPager活动的值发送到一个按片段分组

我有一个ViewPager活动调用表示幻灯片布局的片段。

我需要的是将活动的值通过捆绑传递给片段。 我怎么能做到这一点? 我尝试在onCreate()传递一个int 2,但不工作,我

活动:

@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_slide); Intent intent = new Intent(this,ScreenSlidePageFragment.class); Bundle bundle = new Bundle(); bundle.putInt("SIDE",2); intent.putExtras(bundle); /** * Instantiate a ViewPager and a PagerAdapter */ mPager = (ViewPager)findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { if(itemOld > arg0){ auxBack--; SIDE=1; } else if(itemOld < arg0){ auxFront++; SIDE=0; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); } @Override public void onBackPressed(){ if(mPager.getCurrentItem()==0){ super.onBackPressed(); }else{ mPager.setCurrentItem(mPager.getCurrentItem()-1); } } /** * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in sequence */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{ public ScreenSlidePagerAdapter(FragmentManager fm){ super(fm); } @Override public ScreenSlidePageFragment getItem(int arg0) { return new ScreenSlidePageFragment(); } @Override public int getCount() { //Toast.makeText(getApplicationContext(), "Num pages " + NUM_PAGES, Toast.LENGTH_LONG).show(); return NUM_PAGES; } } 

}

分段:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { int tID = ((ScreenSlidePagerActivity)getActivity()).getIntent().getExtras().getInt("SIDE"); Toast.makeText(getActivity(), "Side selected....." +tID, Toast.LENGTH_LONG).show(); 

Solutions Collecting From Web of "将ViewPager活动的值发送到一个按片段分组"

你必须通过setArguments()方法设置你的Bundle,而不是意图。 如果你有一个ViewPager,你可以将你的parameter passing给PagerAdapter,并在实例化Fragment时设置参数。

我不明白你的SIDE是如何计算的,这是什么意思,所以我展示了一般的ide。

 @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen_slide); Bundle bundle = new Bundle(); bundle.putInt("SIDE",2); /** * Instantiate a ViewPager and a PagerAdapter */ mPager = (ViewPager)findViewById(R.id.pager); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), bundle); mPager.setAdapter(mPagerAdapter); } /** * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in sequence */ private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{ private final Bundle fragmentBundle; public ScreenSlidePagerAdapter(FragmentManager fm, Bundle data){ super(fm); fragmentBundle = data; } @Override public ScreenSlidePageFragment getItem(int arg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Override public int getCount() { return NUM_PAGES; } } 

分段

 @Override public void onCreate(Bundle state) { super.onCreate(state); final Bundle args = getArguments(); final int side = args.getInt("SIDE"); } 

这可能不是你正在要求的解决scheme,但这是我所做的,它可能有助于某人:

在片段中创build一个公共方法:

 public void doSomething(String myValue){ // Do stuff in your fragment with myValue } 

在您的活动中,从适配器中获取片段。 你知道它是基于它被添加的顺序的片段。执行你的片段方法,传递你的variables:

 // Get FriendsFragment (first fragment) MyFragment fragment = (MyFragment) mAdapter.getItem(0); fragment.doSomething("some-value");