如何在当前位置ViewPager中获取Fragment对象

时间:2015-12-30 09:15:21

标签: android events android-fragments android-viewpager

我有一个用3个菜单实现ViewPager的活动(菜单1,菜单2,菜单3)。这是我的代码:

public class MyActivity .... {

    PagerAdapter pa;
    TabLayout tabLayout;
    ViewPager vp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        vp = (ViewPager) findViewById(R.id.viewPager);
        pa = new PageAdapter(getSupportFragmentManager(), getApplicationContext());
        vp.setAdapter(pa);

        tabLayout = (TabLayout) findViewById(R.id.viewPagerMenu);
        tabLayout.setTabsFromPagerAdapter(pa);
        tabLayout.setupWithViewPager(vp);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                int position = vp.getCurrentItem();

                if(position == 0){
                    // I want to take the fragment object that instantiated in this position, to access variable from it's object

                }
            }
        });
    }
}   

public class Fragment1 extends Fragment ... {

    // I want to get Boolean variable from this fragment

    private Boolean toggleSomething = false;
    private Context context;
    private String pageTitle;

    public static Fragment1 newInstance(String pageTitle, Context context){
        Fragment1 frag = new Fragment1();
        frag1.setPageTitle(pageTitle);
        frag1.setContext(context);

        return frag;
    }

    public Boolean getToggleSomething(){
        return this.toggleSomething;
    }

    public void setToggleSomething(Boolean toggleSomething){
        this.toggleSomething = toggleSomething;
    }

    public String getPageTitle(){
        return this.pageTitle;
    }

    public void setPageTitle(String pageTitle){
        this.pageTitle = pageTitle;
    }

    public Context getContext(){
        return this.context;
    }

    public void setContext(Context context){
        this.context = context;
    }

}       

这是我的PageAdapter:

public class PageAdapter extends FragmentPagerAdapter {

    private static int totalMenu = 3;
    private Context context;

    public PageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return Fragment1.newInstance("Menu 1", context);
            case 1:
                return Fragment2.newInstance("Menu 2", context);
            case 2:
                return Fragment3.newInstance("Menu 3", context);
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return totalMenu;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return "Menu 1";
            case 1:
                return "Menu 2";
            case 2:
                return "Menu 3";
            default:
                return null;
        }
    }
}

我的问题是:

我在片段中有一个布尔值,当我选择的选项卡是第一个选项卡时,我希望从我的活动中获取该值。我怎么能这样做?

我想我需要让片段的对象然后用getter访问变量,有什么想法吗?还是有其他方法可以做到这一点?

任何答案都将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:0)

你必须在项目选择

上传递bool值
public Fragment1 newInstance(String pageTitle, Context context,Boolean BOOL_VALUE){
    Fragment1 frag = new Fragment1();
    frag1.setPageTitle(pageTitle);
    frag1.setContext(context);
    frag1.setBoolValue(BOOL_VALUE);
    return frag;
}

并更改构造函数以恢复布尔值

{{1}}
希望这会有所帮助。

答案 1 :(得分:0)

使用静态变量

创建公共类
public class PublicValue{
   public static yourboolean = false;//default values
}

在片段中,您可以将值设置为静态变量

PublicValue.yourboolean = value_you_want use;

然后使用静态变量

@Override
            public void onPageScrollStateChanged(int state) {
                int position = vp.getCurrentItem();

                if(position == 0){
                    // I want to take the fragment object that instantiated in this position, to access variable from it's object
                  if( PublicValue.yourboolean){

                }
            }
        });