通过捆绑将值从 ViewPager Activity 发送到 Fragment

Send values from ViewPager Activity to a Fragment by bundle(通过捆绑将值从 ViewPager Activity 发送到 Fragment)

本文介绍了通过捆绑将值从 ViewPager Activity 发送到 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewPager Activity,它调用代表幻灯片布局的片段.

I have an ViewPager Activity that call the fragment that represent the slide layout.

我需要的是通过捆绑将值从活动传递到片段.我怎么能这样做?我尝试在 onCreate() 中传递一个 int 2 但不起作用,我

What i need is pass values from activity to fragment by bundle. How i can do this? I try pass a int 2 in onCreate() but not work and i

活动:

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

推荐答案

你必须通过 setArguments() 方法来设置你的 Bundle,而不是意图.如果您有 ViewPager,则可以将参数传递给 PagerAdapter,并在实例化 Fragment 时设置参数.

You have to set your Bundle through setArguments() method, not the intent. If you have a ViewPager, you can pass your arguments to the PagerAdapter, and set the arguments when the Fragment is being instantiated.

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

I don't get how your SIDE is calculated and what does it mean, so I show the general ide.

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen_slide);
    Bundle bundle = new Bundle();
    bundle.putInt("SIDE",2);
    mPager = (ViewPager)findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), bundle);
    mPager.setAdapter(mPagerAdapter);
}


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");
}

这篇关于通过捆绑将值从 ViewPager Activity 发送到 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过捆绑将值从 ViewPager Activity 发送到 Fragment

基础教程推荐