何时引用在ViewPager中创建的片段

时间:2014-06-04 11:09:47

标签: android android-fragments android-viewpager

我有一个ViewPager,它将两个片段设置为可滑动的标签。我试图在托管活动中找到这些片段。

我将它们设置为类全局变量:

private SharedBillFragment sharedBill;
private SeparateBillFragment separateBill;

并尝试在活动的onStart()中找到它们:

@Override
protected void onStart() {
    super.onStart();

    findFragments();
}

其中findFragments()是(我知道这有点像黑客,但它现在应该有用......):

private void findFragments() {
    sharedBill = (SharedBillFragment)
            getFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + 0);
    separateBill = (SeparateBillFragment)
            getFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + 1);
}

但这不起作用,找不到碎片。当我在稍后调用的方法中调用findFragments()时(在用户交互之后),找到片段并且我可以使用它们。示例:

private void setCountryTip(String selectedCountry) {
    findFragments();

    sharedBill.setPercentage(getCountryTip(selectedCountry));
    separateBill.setPercentage(getCountryTip(selectedCountry));
}

但即使被调用,片段也仅在调用findFragments()的方法中可用,即使我将它们设置为类全局变量。

最好的情况是,我想在启动应用程序时找到片段并将它们存储在类全局变量中。任何帮助如何实现?

编辑: 有关如何设置片段的更多信息。在我的MainActivity中,我有:

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

    String[] mTabTitles = getResources().getStringArray(R.array.tabs);
    final ActionBar actionBar = getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(getFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    mViewPager.setAdapter(mTabsAdapter);

    for (String tab_name : mTabTitles) {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }
}

和我的TabsAdapter:

public class TabsAdapter extends FragmentPagerAdapter {
final int NUMBER_OF_TABS = 2;

public TabsAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {
    switch (index) {
        case 0:
            return new SharedBillFragment();
        case 1:
            return new SeparateBillFragment();
    }

    return null;
}

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

编辑:关于NPE,我的微调器在片段中设置如下:

private Spinner spCountry;

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    spCountry = (Spinner) getActivity().findViewById(R.id.spCountry);
    spCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            mCallback.onCountrySelected(spCountry.getItemAtPosition(pos).toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
}

public void setCountry(String country) {
    ArrayAdapter spCountryAdapter = (ArrayAdapter) spCountry.getAdapter();
    spCountry.setSelection(spCountryAdapter.getPosition(country));
}

微调器通过xml属性获取其条目&#34; android:entries&#34;。

1 个答案:

答案 0 :(得分:0)

不是一个非常好的和可重复使用的解决方案,但由于您使用静态片段,它可以工作:使适配器键入Fragment,并初始化活动中的片段。然后将两个片段添加到适配器,并在适配器中getItem函数只返回相应的片段。

适配器:

public class TabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;

public TabsAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int index) {
    return fragments.get(index);
}

@Override
public int getCount() {
    return fragments.size();
}
}

的活动:

    sharedBillFragment = new SharedBillFragment();
    separateBillFragment = new SeparateBillFragment();
    List<Fragment> fragments = new ArrayList<Fragment>();
    fragments.add(sharedBillFragment);
    fragments.add(separateBillFragment);
    mTabsAdapter = new TabsAdapter(getFragmentManager(), fragments);
    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    mViewPager.setAdapter(mTabsAdapter);

在onCreateView中找到像这样的微调器:

public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View v = inflater.inflate(R.layout.fragment, containter, false);
   spCountry = (Spinner) getActivity().findViewById(R.id.spCountry);
   ...
   return v;
}