将数据传递到ViewPager和FragmentPagerAdapter之外的片段中

时间:2015-04-14 05:36:48

标签: android android-fragments android-viewpager fragmentpageradapter

我想要做的是,将一个排序变量传递给特定的ViewPager片段

我的ViewPager正常工作。我使用OnPageChangeListener来获取当前片段和当前标签位置。

但是,当我在监听器外传递变量时,它总是变为0 代码:currentPos = arg0;(arg0正在返回正确的位置)

System.out.println("Current:" + currentPos);

所有标签显示结果:Current:0

ThirdTab.java

private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
public Fragment fragment;
public int currentPos;


public String getSorting() {
    return sorting;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.third_tab, container, false);
    setHasOptionsMenu(true);

    tabs = (PagerSlidingTabStrip) rootView.findViewById(R.id.slidingTabStrip);
    pager = (ViewPager) rootView.findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getChildFragmentManager(), getActivity().getApplicationContext());
    adapter.setTitle(catType);
    pager.setAdapter(adapter);
    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
    pager.setPageMargin(pageMargin);
    tabs.setViewPager(pager);

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            System.out.println("Good" + arg0);
            fragment = ((MyPagerAdapter) pager.getAdapter()).getFragment(arg0);
            currentPos = arg0;
            if (arg0 == 1 && fragment != null) {
                fragment.onResume();
            }
        }

        @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 boolean onOptionsItemSelected(MenuItem item) {
    // handle item selection

    switch (item.getItemId()) {
    case R.id.menuSortRecentCreate:
    case R.id.menuSortRecentUpdate:
    case R.id.menuSortLowestPrice:
    case R.id.menuSortHighestPrice:
        if (item.getItemId() == R.id.menuSortRecentCreate) {
            sorting = "0";
        } else if (item.getItemId() == R.id.menuSortRecentUpdate) {
            sorting = "1";
        } else if (item.getItemId() == R.id.menuSortLowestPrice) {
            sorting = "2";
        } else if (item.getItemId() == R.id.menuSortHighestPrice) {
            sorting = "3";
        }

        System.out.println("Current:" + currentPos);
        System.out.println("Current Page:" + pager.getCurrentItem());

        return true;
    case R.id.action_search:
        return true;
    case R.id.action_setting:
        ThirdTabTaskSettingActivity fragment = new ThirdTabTaskSettingActivity();
        mainActivity.pushFragments(getResources().getString(R.string.title_third_tab), fragment, true, true);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

ThirdTab.java中的MyPagerAdapter

 public class MyPagerAdapter extends FragmentStatePagerAdapter {
            private String[] TITLES = {};

            private String[] PERSONAL_TITLES = { getString(R.string.personal_category1), getString(R.string.personal_category2), getString(R.string.personal_category3),
                    getString(R.string.personal_category4), getString(R.string.personal_category5), getString(R.string.personal_category6) };

            private String[] BUSINESS_TITLES = { getString(R.string.business_category1), getString(R.string.business_category2), getString(R.string.business_category3),
                    getString(R.string.business_category4), getString(R.string.business_category5) };

            HashMap<Integer, Fragment> hm = new HashMap<Integer, Fragment>();

            private Map<Integer, String> mFragmentTags;
            private FragmentManager mFragmentManager;
            private Context mContext;

            public MyPagerAdapter(FragmentManager fm, Context context) {
                super(fm);
                mFragmentManager = fm;
                mFragmentTags = new HashMap<Integer, String>();
                mContext = context;
            }

            public void setTitle(String category) {
                if (category.equals("personal")) {
                    TITLES = PERSONAL_TITLES;
                }
                else {
                    TITLES = BUSINESS_TITLES;
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return TITLES[position];
            }

            @Override
            public int getCount() {
                return TITLES.length;
            }

            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;

                switch (position) {
                case 0:
                    fragment = ThirdTabContentFragment1.newInstance(position, sorting, keyword);
                    return fragment;
                case 1:
                    fragment = ThirdTabContentFragment2.newInstance(position, sorting, keyword);
                    return fragment;
                case 2:
                    fragment = ThirdTabContentFragment3.newInstance(position, sorting, keyword);
                    return fragment;
                case 3:
                    fragment = ThirdTabContentFragment4.newInstance(position, sorting, keyword);
                    return fragment;
                case 4:
                    fragment = ThirdTabContentFragment5.newInstance(position, sorting, keyword);
                    return fragment;
                case 5:
                    fragment = ThirdTabContentFragment6.newInstance(position, sorting, keyword);
                    return fragment;
                }
                return fragment;
                // return SuperAwesomeCardFragment.newInstance(position, sorting, keyword);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                System.out.println("instantiate");
                Object obj = super.instantiateItem(container, position);
                if (obj instanceof Fragment) {
                    Fragment f = (Fragment) obj;
                    String tag = f.getTag();
                    mFragmentTags.put(position, tag);
                }
                return obj;
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }

            public Fragment getFragment(int position) {
                System.out.println("In:" + position);
                String tag = mFragmentTags.get(position);
                if (tag == null) { return null; }
                return mFragmentManager.findFragmentByTag(tag);
            }
        }

ReceiveData的片段

public class ThirdTabContentFragment1 extends BaseContainerFragment implements AsyncResponse {

    public static ThirdTabContentFragment1 newInstance(int position, String vSorting, String vKeyword) {
        ThirdTabContentFragment1 f = new ThirdTabContentFragment1();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        System.out.println("a" + position);
        return f;
    }

    @Override
    public void onResume() {
        System.out.println("Resume1");
        super.onResume();
    }

}

0 个答案:

没有答案