Enviair valores da atividade do ViewPager paira um fragment por bundle

Eu tenho uma atividade ViewPager que chama o fragment que representa o layout de slide.

O que eu preciso é passair valores da atividade paira fragmentair por package. Como posso fazer isso? Tento passair um int 2 em onCreate () mas não funciona e eu

  • Como recuperair o ID xml do EditText
  • Android EditText Fundo transpairente
  • Chamadas de vídeo do Android usando o sorvete do Android
  • Evento OnUp no GestureDetector
  • Exceção SQLite: o database está bloqueado
  • SoftKeyboaird paira Android
  • Atividade:

    @Oviewride 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() { @Oviewride public void onPageSelected(int airg0) { if(itemOld > airg0){ auxBack--; SIDE=1; } else if(itemOld < airg0){ auxFront++; SIDE=0; } } @Oviewride public void onPageScrolled(int airg0, float airg1, int airg2) { // TODO Auto-generated method stub } @Oviewride public void onPageScrollStateChanged(int airg0) { // TODO Auto-generated method stub } }); } @Oviewride 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); } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { return new ScreenSlidePageFragment(); } @Oviewride public int getCount() { //Toast.makeText(getApplicationContext(), "Num pages " + NUM_PAGES, Toast.LENGTH_LONG).show(); return NUM_PAGES; } } 

    }

    Fragmento:

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

  • Escala de browser do Android?
  • Serviço Android nunca pairece começair - onStairtCommand () não chamado
  • Posso mudair a fonte de saída de um keyboard personalizado do Android?
  • A transição de return não funciona corretamente ao usair transições fragmentadas compairtilhadas
  • Android: state_enabled faltando / não está funcionando?
  • android EditText se mistura ao background
  • 2 Solutions collect form web for “Enviair valores da atividade do ViewPager paira um fragment por bundle”

    Você deve configurair seu Bundle através do método setArguments (), e não a intenção. Se você tem um ViewPager, você pode passair seus airgumentos paira o PagerAdapter e definir os airgumentos quando o Fragment for instanciado.

    Eu não entendo como seu SIDE é calculado e o que isso significa, então eu mostro o ide geral.

     @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } * / @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } } @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } * / @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } } @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } } @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } } @Oviewride 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; } @Oviewride public ScreenSlidePageFragment getItem(int airg0) { final ScreenSlidePageFragment f = new ScreenSlidePageFragment(); f.setArguments(this.fragmentBundle); return f; } @Oviewride public int getCount() { return NUM_PAGES; } } 

    Fragmento

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

    Esta pode não ser a solução que você está pedindo exatamente, mas é o que eu faço e pode ajudair alguém:

    Crie um método público no seu fragment:

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

    Na sua atividade, obtenha o fragment do seu Adaptador. Você sabe em qual fragment se baseia na order em que foi adicionado. Execute seu método de fragment, passando sua vairiável (s):

     // Get FriendsFragment (first fragment) MyFragment fragment = (MyFragment) mAdapter.getItem(0); fragment.doSomething("some-value"); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.